views:

80

answers:

4

my question is shown in this code

i have class like that

public class  maincs
{
  public int a;
  public int b;
  public int c;
  public int d; 
}

public class  sub1
{
  public int a;
  public int b;
  public int c;
}


public void methoda (sub1 model)
{
  maincs mdata = new maincs(){a = model.a , b = model.b , c= model.c} ;   

  // is there is away to directly cast class sub1 into main like that    
  mdata = (maincs) model;    
}
+1  A: 

You could change your class structure to:

public class maincs : sub1
{
   public int d; 
}

public class sub1
{
   public int a;
   public int b;
   public int c;
}

Then you could keep a list of sub1 and cast some of them to mainc.

Jake Pearson
This doesn't compile either. Maybe you forgot the `class` keyword
Carlos Muñoz
Oops, that's what I get for copy/paste.
Jake Pearson
+4  A: 

You have already defined the conversion, you just need to take it one step further if you would like to be able to cast. For example:

public class sub1
{
    public int a;
    public int b;
    public int c;

    public static explicit operator maincs(sub1 obj)
    {
        maincs output = new maincs() { a = obj.a, b = obj.b, c = obj.c };
        return output;
    }
}

Which then allows you to do something like

static void Main()
{
    sub1 mySub = new sub1();
    maincs myMain = (maincs)mySub;
}
Anthony Pegram
A: 

You can provide an explicit overload for the cast operator:

public static explicit operator maincs(sub1 val)
{
    var ret = new maincs() { a = val.a, b = val.b, c = val.c };
    return ret;
}

Another option would be to use an interface that has the a, b, and c properties and implement the interface on both of the classes. Then just have the parameter type to methoda be the interface instead of the class.

Chris Shaffer
+1  A: 

what he wants to say is

if you have two classes which shares most of the properties can you cast an object from class a to class b and automatically make the system understand the assignment by shared properties names ?

option1: use reflection

disadvantage : gonna slow you down more than you think.

option2: make one derive from another and have one with common properties and other extend that

disadvantage: coupled ! if your doing that for 2 layers in your application then the 2 layers will be coupled


not sure if that what you mean !! correct me if im wrong


Let there be :

    class customer
{
    public string firstname { get; set; }
    public string lastname { get; set; }
    public int age { get; set; }
}
class employee
{
    public string firstname { get; set; }
    public int age { get; set; } 
}

now here is an extension for Object type

       public static T Cast<T>(this Object myobj)
   {
       Type objectType = myobj.GetType();
       Type target = typeof(T);
       var x = Activator.CreateInstance(target, false);
       var z = from source in objectType.GetMembers().ToList() where source.MemberType == MemberTypes.Property select source ;
       var d = from source in target.GetMembers().ToList() where source.MemberType == MemberTypes.Property select source;
       List<MemberInfo> members = d.Where(memberInfo => d.Select(c => c.Name).ToList().Contains(memberInfo.Name)).ToList();
       PropertyInfo propertyInfo;
       object value;
       foreach (var memberInfo in members)
       {
           propertyInfo = typeof(T).GetProperty(memberInfo.Name);
           value = myobj.GetType().GetProperty(memberInfo.Name).GetValue(myobj,null);

           propertyInfo.SetValue(x,value,null);
       }
       return (T)x;
   }

now you use it like this :

   static void Main(string[] args)
    {
        var cus = new customer();
        cus.firstname = "John";
        cus.age = 3;
        employee emp =  cus.Cast<employee>();
    }

method cast check common properties between two object and do the assignment and return new object all ready.

Stacker
That what i mean exactly
Khalid Omar