将类转换为另一个类或将类转换为另一个类 [英] cast class into another class or convert class to another
问题描述
我的问题显示在此代码中
我有类似的
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}
//有一种方法可以直接将类sub1转换为main类似
mdata =(maincs)模型;
}
:
如果你有两个类共享大多数相同的属性,你可以从类 a c>到类
b
并自动使系统通过共享属性名称来理解赋值?
选项1:反射
缺点:它会比你想象的更慢。
选项2:从另一个,第一个与共同的属性和其他的延伸。
缺点:耦合!如果你的应用程序中有两层,那么这两层将会耦合!
让你有:
class customer
{
public string firstname {get;组; }
public string lastname {get;组; }
public int age {get;组; }
}
class employee
{
public string firstname {get;组; }
public int age {get;组; }
}
现在这里是对象类型的扩展:
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 object in objectType.GetMembers()。ToList()
其中source.MemberType == MemberTypes.Property select source;
var d = from target in target.GetMembers()。ToList()
其中source.MemberType == MemberTypes.Property select source;
List< MemberInfo> member = d.Where(memberInfo => d.Select(c => c.Name)
.ToList()。Contains(memberInfo.Name))。ToList();
PropertyInfo propertyInfo;
对象值;
foreach(成员中的var memberInfo)
{
propertyInfo = typeof(T).GetProperty(memberInfo.Name);
value = myobj.GetType()。GetProperty(memberInfo.Name).GetValue(myobj,null);
propertyInfo.SetValue(x,value,null);
}
return(T)x;
}
现在,您可以这样使用:
static void Main(string [] args)
{
var cus = new customer
cus.firstname =John;
cus.age = 3;
employee emp = cus.Cast< employee>();
}
方法转换检查两个对象之间的公共属性, p>
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 a way 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屋!