cast class into another class or convert class to another

前端 未结 9 604
逝去的感伤
逝去的感伤 2020-11-28 22:29

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; 
}
         


        
相关标签:
9条回答
  • 2020-11-28 22:57

    By using following code you can copy any class object to another class object for same name and same type of properties.

    public class CopyClass
    {
        /// <summary>
        /// Copy an object to destination object, only matching fields will be copied
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sourceObject">An object with matching fields of the destination object</param>
        /// <param name="destObject">Destination object, must already be created</param>
        public static void CopyObject<T>(object sourceObject, ref T destObject)
        {
            //  If either the source, or destination is null, return
            if (sourceObject == null || destObject == null)
                return;
    
            //  Get the type of each object
            Type sourceType = sourceObject.GetType();
            Type targetType = destObject.GetType();
    
            //  Loop through the source properties
            foreach (PropertyInfo p in sourceType.GetProperties())
            {
                //  Get the matching property in the destination object
                PropertyInfo targetObj = targetType.GetProperty(p.Name);
                //  If there is none, skip
                if (targetObj == null)
                    continue;
    
                //  Set the value in the destination
                targetObj.SetValue(destObject, p.GetValue(sourceObject, null), null);
            }
        }
    }
    

    Call Method Like,

    ClassA objA = new ClassA();
    ClassB objB = new ClassB();
    
    CopyClass.CopyObject(objOfferMast, ref objB);
    

    It will copy objA into objB.

    0 讨论(0)
  • 2020-11-28 23:04

    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.

    0 讨论(0)
  • 2020-11-28 23:10

    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;
    }
    
    0 讨论(0)
  • 2020-11-28 23:12

    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.

    0 讨论(0)
  • 2020-11-28 23:13
    var obj =  _account.Retrieve(Email, hash);
    
    AccountInfoResponse accountInfoResponse = new AccountInfoResponse();
    
    if (obj != null)
    {               
       accountInfoResponse = 
       JsonConvert.
       DeserializeObject<AccountInfoResponse>
       (JsonConvert.SerializeObject(obj));
    }
    

    image description

    0 讨论(0)
  • 2020-11-28 23:17

    Use JSON serialization and deserialization:

    using Newtonsoft.Json;
    
    Class1 obj1 = new Class1();
    Class2 obj2 = JsonConvert.DeserializeObject<Class2>(JsonConvert.SerializeObject(obj1));
    

    Or:

    public class Class1
    {
        public static explicit operator Class2(Class1 obj)
        {
            return JsonConvert.DeserializeObject<Class2>(JsonConvert.SerializeObject(obj));
        }
    }
    

    Which then allows you to do something like

    Class1 obj1 = new Class1();
    Class2 obj2 = (Class2)obj1;
    
    0 讨论(0)
提交回复
热议问题