VisualBean
VisualBean

Reputation: 5008

Convert object to another

I have a class which is part of an assembly i'm referencing. I want to convert Object of that type to my own class that implements the class i'm referencing

Lets say my reference has

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

and i've created

public class mycustomer : customer
{
 public string name {get; set}
 public string address{get;set}
 public string email {get;set}
}

How do i convert customer to mycustomer and back I've read about using reflections, but i'm not comfortable enough with it, to actually write it myself.

PS. please stop with the naming convention semantics - this is a rough on the fly theoretical example (not bothering with naming conventions here, only in actual code) Thanks in advance

Edit: just figured out i cannot do this anyway. i need to serialize an object with doesnt have the serializable attribute, and i thought i could just mirror that class and make it serializable - but i just realised that some of the properties inside this class doesn't have the serializable attribute.

so thanks anyway - i'll mark best answer to the question as answer /Alex

Upvotes: 0

Views: 8502

Answers (4)

vnthanhnv
vnthanhnv

Reputation: 1

Simple way!

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

"Cast" method implement follow this video https://www.youtube.com/watch?v=XUqfg9albdA

Upvotes: 0

dcastro
dcastro

Reputation: 68750

No need to write it yourself. You can copy a Customer's properties to a MyCustomer object using reflection with this generic algorithm:

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

I think using a full blown library like AutoMapper for just one scenario is a bit overkill.

Upvotes: 3

Sergey Berezovskiy
Sergey Berezovskiy

Reputation: 236328

mycustomer already have members inherited from customer. Do not hide those members:

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

Now mycustomer IS A customer, and there is no problems with this conversion - just assign instance of mycustomer to variable of customer type:

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

Converting them back is strange, thus customer do not have email property and it will not appear - you still will have only data provided by base type, so simply use base type here. But if customer is actually a mycustomer instance (see code above) then all you need is casting:

mycustomer mc2 = (mycustomer)c;

BTW in C# we use PascalNaming for type names and public members.

Upvotes: 3

Ehsan
Ehsan

Reputation: 32719

Automapper is there to help you out.

or if you have only class then it is fairly simple to write one of your own.

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

You should not however that you don't need inheritance to map.

public class mycustomer : customer

should be

public class mycustomer

You should also use this naming convention

public class MyCustomer
{
   public string Name {get; set}
   public string Address{get;set}
   public string Email {get;set}
}

Upvotes: 3

Related Questions