0

我有一个类,它是我引用的程序集的一部分。我想将该类型的 Object 转换为我自己的实现我正在引用的类的类

可以说我的参考有

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

我如何将客户转换为我的客户并返回我已经阅读过有关使用反射的信息,但我对它不够满意,无法自己编写。

PS。请停止命名约定语义 - 这是一个粗略的即时理论示例(此处不关心命名约定,仅在实际代码中)提前致谢

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

无论如何,谢谢-我会将问题的最佳答案标记为答案/Alex

4

4 回答 4

3

Automapper可以帮助您。

或者,如果您只有课程,那么编写自己的课程相当简单。

private mycustomer(customer c)
{
    return new mycustomer { name = c.Name, address = c.address,email = c.email };
}

但是,您不应该不需要继承来 map

public class mycustomer : customer

应该

public class mycustomer

您还应该使用命名约定

public class MyCustomer
{
   public string Name {get; set}
   public string Address{get;set}
   public string Email {get;set}
}
于 2013-08-30T09:42:01.153 回答
3

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是 A customer,并且这种转换没有问题 - 只需将实例分配给类型mycustomer变量customer

mycustomer mc = new mycustomer();
customer c = mc;

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

mycustomer mc2 = (mycustomer)c;

顺便说一句,在 C# 中,我们将 PascalNaming 用于类型名称和公共成员。

于 2013-08-30T09:47:37.017 回答
3

不需要自己写。您可以使用具有此通用算法的反射将客户的属性复制到 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 这样成熟的库有点矫枉过正。

于 2013-08-30T10:09:43.240 回答
0

简单的方法!

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

“演员”方法实现跟随此视频 https://www.youtube.com/watch?v=XUqfg9albdA

于 2016-05-31T16:47:52.060 回答