C# 将对象转换为另一个对象

C# 将对象转换为另一个对象,c#,type-conversion,C#,Type Conversion,我有一个类,它是我引用的程序集的一部分。 我想转换那个类型的对象 我自己的类实现了我引用的类 假设我的推荐人 public class customer { public string name {get;set} public string address{get;set} } 我创造了 public class mycustomer : customer { public string name {get; set} public string address{get;set} pu

我有一个类,它是我引用的程序集的一部分。 我想转换那个类型的对象 我自己的类实现了我引用的类

假设我的推荐人

public class customer
{
public string name {get;set}
public string address{get;set}
}
我创造了

public class mycustomer : customer
{
 public string name {get; set}
 public string address{get;set}
 public string email {get;set}
}
如何将客户转换为mycustomer并返回 我读过关于使用反射的书,但我对它不太适应,不能自己写

请停止使用命名约定语义-这是一个粗略的动态理论示例,这里不涉及命名约定,仅在实际代码中 提前谢谢

编辑:我想我无论如何都做不到。 我需要序列化一个没有serializable属性的对象,我想我可以镜像该类并使其可序列化-但我刚刚意识到该类中的某些属性没有serializable属性

所以无论如何,谢谢你——我会把这个问题的最佳答案标记为答案 /亚历克斯是来帮你的

或者,如果您只有一个类,那么编写一个自己的类是相当简单的

private mycustomer(customer c)
{
    return new mycustomer { name = c.Name, address = c.address,email = c.email };
}
然而,您不应该认为映射不需要继承

应该是

public class mycustomer
你也应该用这个

mycustomer已从customer继承了成员。不要隐藏这些成员:

public class customer
{
    public string name { get; set; }
    public string address { get; set; }
}

public class mycustomer : customer
{ 
    // name and address are inherited
    public string email { get; set; }
}
现在mycustomer是一个客户,此转换没有问题-只需将mycustomer的实例分配给customer类型的变量:

将它们转换回来是很奇怪的,所以客户并没有电子邮件属性,它也不会出现-您仍然只有基本类型提供的数据,所以只需在这里使用基本类型。但如果customer实际上是mycustomer实例,请参见上面的代码,那么您所需要的只是强制转换:

mycustomer mc2 = (mycustomer)c;

顺便说一句,在C语言中,我们对类型名和公共成员使用PascalNaming。

无需自己编写。您可以使用以下通用算法使用反射将客户的属性复制到MyCustomer对象:

    public B Convert<A, B>(A element) where B : A, new()
    {
        //get the interface's properties that implement both a getter and a setter
        IEnumerable<PropertyInfo> properties = typeof(A)
            .GetProperties()
            .Where(property => property.CanRead && property.CanWrite).ToList();

        //create new object
        B b = new B();

        //copy the property values to the new object
        foreach (var property in properties)
        {
            //read value
            object value = property.GetValue(element);

            //set value
            property.SetValue(b, value);
        }

        return b;
    }
我认为在一个场景中使用像AutoMapper这样成熟的库有点过分。

简单的方法

public class ClassA
    {
        public int id { get; set; }
        public string name { get; set; }
        public int age { get; set; }
        public string note { get; set; }
    }

 public class ClassB
    {
        public int id { get; set; }
        public string name { get; set; }
        public int age { get; set; }
        public string note { get; set; }
        public int index { get; set; }
    }

static void Main(string[] args)
        {
           //create an object with ClassA  type
            ClassA a = new ClassA { id=1,age=12,name="John",note="good"};
            ClassB b=a.Cast<ClassB>();
        }
下面是视频

这里的问题其实不是命名约定,因为我的示例纯粹是理论性的。我需要序列化没有可序列化属性[serialize]的客户因此,我正在创建我的代理类,而代理类又具有可序列化属性,这就是为什么我要继承以确保兼容性。如果我假设错误,请纠正我here@AlekCarlsen世界上几乎每个.net开发人员都使用相同的命名约定。这一点在你提出的每一个问题中都会被提及,我也是。。但不是以粗略的理论为例,避免了不必要的继承。重构customer并添加电子邮件属性。使用null或string.Empty表示客户没有。
mycustomer mc2 = (mycustomer)c;
    public B Convert<A, B>(A element) where B : A, new()
    {
        //get the interface's properties that implement both a getter and a setter
        IEnumerable<PropertyInfo> properties = typeof(A)
            .GetProperties()
            .Where(property => property.CanRead && property.CanWrite).ToList();

        //create new object
        B b = new B();

        //copy the property values to the new object
        foreach (var property in properties)
        {
            //read value
            object value = property.GetValue(element);

            //set value
            property.SetValue(b, value);
        }

        return b;
    }
public class ClassA
    {
        public int id { get; set; }
        public string name { get; set; }
        public int age { get; set; }
        public string note { get; set; }
    }

 public class ClassB
    {
        public int id { get; set; }
        public string name { get; set; }
        public int age { get; set; }
        public string note { get; set; }
        public int index { get; set; }
    }

static void Main(string[] args)
        {
           //create an object with ClassA  type
            ClassA a = new ClassA { id=1,age=12,name="John",note="good"};
            ClassB b=a.Cast<ClassB>();
        }