投类到另一个类或类转换到另一个 [英] cast class into another class or convert class to another

查看:161
本文介绍了投类到另一个类或类转换到另一个的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是显示在此code

我有这样的类

 公共类maincs
{
  公众诠释一个;
  公众诠释B:
  公众诠释℃;
  公众诠释D组;
}公共类SUB1
{
  公众诠释一个;
  公众诠释B:
  公众诠释℃;
}
公共无效了methodA(SUB1模型)
{
  maincs MDATA =新maincs(){a = model.a,B = model.b,C = model.c};  //是有去直接浇铸类SUB1到主一样,
  MDATA =(maincs)模型;
}


解决方案

他想要说的是:

如果您有共享大多数相同属性的两个类你可以施放从类对象 A B ,并自动使系统了解通过共享属性名称赋值?

选项1:使用反射

缺点:这会减慢你的速度比你想象

选项2:使一个类从另一派生,第一个具有共同属性等的那一个扩展

缺点:再加!如果your're这样做,在你的应用程序,然后两层将被耦合两层!

让那里是:

 类客户
{
    公共字符串名字{获得;组; }
    公共字符串名字{获得;组; }
    公众诠释年龄{搞定;组; }
}
类员工
{
    公共字符串名字{获得;组; }
    公众诠释年龄{搞定;组; }
}

现在这里是Object类型的扩展名:

 公共静态牛逼演员LT; T>(此对象MyObj中)
{
    键入的objectType = myobj.GetType();
    目标类型= ty​​peof运算(T);
    VAR X = Activator.CreateInstance(目标,FALSE);
    变种Z =从objectType.GetMembers()源。了ToList()
        其中,source.MemberType == MemberTypes.Property选择源;
    变种D =从target.GetMembers()源。了ToList()
        其中,source.MemberType == MemberTypes.Property选择源;
    清单<&的MemberInfo GT;成员= d.Where(的MemberInfo => d.Select(C => c.Name)
       .ToList()包含(memberInfo.Name))了ToList()。
    的PropertyInfo的PropertyInfo;
    对象值;
    的foreach(在成员VAR的MemberInfo)
    {
        的PropertyInfo = typeof运算(T).GetProperty(memberInfo.Name);
        值= myobj.GetType()的getProperty(memberInfo.Name).GetValue(MyObj中,NULL);        propertyInfo.SetValue(X,值null);
    }
    返程(T)×;
}

现在你使用这样的:

 静态无效的主要(字串[] args)
{
    VAR CUS =新客户();
    cus.firstname =约翰;
    cus.age = 3;
    员工EMP = cus.Cast<员工>();
}

与两个物体之间

法铸造检查公共属性会自动分配。

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

解决方案

What he wants to say is:

"If you have two classes which share most of the same properties you can cast an object from class a to class b and automatically make the system understand the assignment via the shared property names?"

Option 1: Use reflection

Disadvantage : It's gonna slow you down more than you think.

Option 2: Make one class derive from another, the first one with common properties and other an extension of that.

Disadvantage: Coupled! if your're doing that for two layers in your application then the two layers will be coupled!

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 checks common properties between two objects and does the assignment automatically.

这篇关于投类到另一个类或类转换到另一个的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆