-3

I have 2 classes which are exactly same except the name. I do not have access to either of the class to change anything in it. In my code, I need to copy one of one class to another class. How can I do this?

class A
{
     string a;
}

class b
{
     string a;
}

     a a1=new a();
     b b1=new b1();
     a1=b1;

I want to assign one class object to another class object in c# code.

MatthewD
  • 6,719
  • 5
  • 22
  • 41
faiee
  • 11
  • 1
  • 1
  • You can use dynamics, automapper, explicit setting, Select projections when dealing with collections. There are lots of ways to do this. – lintmouse Oct 12 '15 at 19:53
  • 1
    Note that you can't set an object to another object. You assign objects to variables, not other objects. – Servy Oct 12 '15 at 19:56
  • You might want to look at mapping libraries like AutoMapper – juharr Oct 12 '15 at 20:07

3 Answers3

4

There is no way for you to assign an instance of A to a variable of type b (or the reverse), in the code that you showed. The fact that the classes have the same fields doesn't mean that one can be implicitly converted to the other. The best that you could do, without being able to change either type, is to create a method that takes one type and creates a new instance of the other type, copying over the relevant value(s).

Servy
  • 202,030
  • 26
  • 332
  • 449
3

Serialize ObjectA to a json string, then serialize that into ObjectB.

Dustin_00
  • 345
  • 2
  • 8
  • 1
    That's not actually assigning one instance to a variable of a different type, as the OP is asking for, that's a way of creating a new object. Next, if he *does* want to create a new instance, it's far easier to just create a new instance, rather than serializing it. And then of course there's the fact that plenty of objects aren't serializable. – Servy Oct 12 '15 at 20:06
  • As all the answers here are hack options, having a range of them gives you a choice of what works in your situation. Right now, I'm looking at a data model object that is 80 properties (no methods). I'd rather copy-through-json than hand wire each property over to the new object. – Dustin_00 Oct 13 '15 at 15:55
  • The *answer* is that it's not possible. It is the only answer to the actual question that was asked. The only way to move the data from the one variable to another is to create a new object instance and copy over the relevant data. How you go about doing that can vary, in that you could potentially use any number of tools to help you do it, (although it's pretty clear that this one isn't suitable to the specific example given) but that core answer is missing from your post. – Servy Oct 13 '15 at 16:06
0

It sounds like you may be asking about casting? Like others mentioned, this isn't going to work on classes themselves. You can loop through each object/string in the class to be converted and cast each object that way. That seems to be what you're wanting to ultimately accomplish.

 // Create a new derived type.
 Giraffe g = new Giraffe();

 // Implicit conversion to base type is safe.
 Animal a = g;

 // Explicit conversion is required to cast back
 // to derived type. Note: This will compile but will
 // throw an exception at run time if the right-side
 // object is not in fact a Giraffe.
 Giraffe g2 = (Giraffe) a;

Source: https://msdn.microsoft.com/en-us/library/ms173105.aspx

Rafiki
  • 630
  • 1
  • 8
  • 22
  • Given that the classes do not inherit from each other, and do not define the implicit or explicit operators, this would not work. – juharr Oct 12 '15 at 20:02
  • Edited to clarify that. – Rafiki Oct 12 '15 at 20:04
  • Now the code doesn't really match up with what you're saying. Servy pretty much covered this by saying the OP could write a method that takes one class type and returns the other doing the correct mapping. – juharr Oct 12 '15 at 20:06
  • I did say "Like others mentioned". To me it seemed like he was wanting to "cast" objects since this appears to be a programming assignment. That's why I posted an MSDN article that references casting. I agree that Servy's answer addresses the question as asked, but that doesn't inhibit me from providing insight that might help others who are searching SO for copying objects. I'm here trying to help not feed trolls. – Rafiki Oct 12 '15 at 20:11