Need solution regarding generic c# method -


i trying make method generic , stuck @ point , need assistance. code scenario have abstract class mybaseabs contains common properties:

public abstract class mybaseabs {     public string commonprop1 { get; set; }     public string commonprop2 { get; set; }     public string commonprop3 { get; set; } } 

now have child classes:

public class mychild1: mybaseabs {     public string mychild1prop1 { get; set; }     public string mychild1prop2 { get; set; }     public string mychild1prop3 { get; set; } } 

and child class:

public class mychild2: mybaseabs {     public string mychild1prop1 { get; set; }     public string mychild2prop2 { get; set; } } 

now have create common method needs perform operations on basis of mychild1 , mychild2, did is:

public mycustomclass saveoperation<t>(t myobj)         t : mybaseabs {     saveobject obj = new saveobject();  } 

so inside method need write common code mapping saveobject object according child object passed. how can determine object passed , use properties accordingly.

one option create base save function in base class , make virtual.

then override method in child classes. way when call save method in saveoperation should call appropriate method correct child class.

public abstract class mybaseabs {     public string commonprop1 { get; set; }     public string commonprop2 { get; set; }     public string commonprop3 { get; set; }      public virtual void save() { } }  public class mychild1: mybaseabs {     public string mychild1prop1 { get; set; }     public string mychild1prop2 { get; set; }     public string mychild1prop3 { get; set; }      public override void save() {          //implementation mychild1     } }  public class mychild2: mybaseabs {     public string mychild1prop1 { get; set; }     public string mychild2prop2 { get; set; }      public override void save() {          //implementation mychild2     } } 

Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -