Take the 2-minute tour ×

Lets assume I have 3 classes A, B and C.

All classes belong to different assemblies, for example:

Class A belongs to assembly ASSEMBLY_A, Class B belongs to assembly ASSEMBLY_B, Class C belongs to assembly ASSEMBLY_C

Also, ASSEMBLY_A references ASSEMBLY_B, ASSEMBLY_C references ASSEMBLY_B, ASSEMBLY_B does not reference any assembly A/B.

That is: ASSEMBLY_A ------> ASSEMBLY_B <-------- ASSEMBLY_C

Class A has a IList custom collection Class C has a IList custom collection

class DataTypeA
    propertyA1 -> string type
    propertyA2 -> int type
    propertyA3 -> bool type

class DataTypeC
    propertyC1 -> string type
    propertyC2 -> int type
    propertyC3 -> bool type

Note that propertyA1 is equivalent to propertyC1, propertyA2 to propertyC2 and propertyA3 to propertyC3.

so I would like to have a helper method in ASSEMBLY_B to do the following:

1) Convert IList from Class A to IList from Class C. 2) Convert IList from Class C to IList from Class A.

How to achieve this?

share|improve this question
Can you not simply create the class in ASSEMBLY_B and get rid of DataTypeA and DataTypeC in lieu of DataTypeB if they are as equivalent as you say? –  McAden May 6 '13 at 22:08
add comment

2 Answers

up vote 1 down vote accepted

You could use generics to accomplish this, but you need an interface.

Create an interface like this in ASSEMBLY_B:

public interface IDataType
    string property1 {get; set;}
    int property2 {get; set; }
    bool property3 {get; set;}

Then create a generic method like this in ASSEMBLY_B:

public Y Convert<T, Y>(T itemToConvert) where T : new(), IDataType where Y : new(), IDataType
    return new Y 
                property1 = itemToConvert.property1,
                property2 = itemToConvert.property2,
                property3 = itemToConvert.property3

Make sure both your classes implement this interface correctly. After that you can call the method like this:

DataTypeA itemToConvert;
// set some values here    
DataTypeC converted = ASSEMBLY_B.Convert<DataTypeA, DataTypeC>(itemToConvert);
share|improve this answer
Great!Just what I want. Thanks a lot! –  user1624552 May 7 '13 at 6:42
add comment

It's impossible: you can't convert an object if you don't have any information on this object. The only way will be to add interfaces in ASSEMBLY_D:

  • IDataTypeA (implemented by DataTypeA)
  • IDataTypeC (implemented by DataTypeC)

Then all assemblies reference ASSEMBLY_D, and ASSEMBLY_C have the information needed to convert DataTypeA to DataType C

share|improve this answer
Not true, check my answer. It is possible with generics and generic constraints –  Kenneth May 6 '13 at 22:42
You made the assumption that both classes can implement the same interface. I didn't made this, as the property names are different (propertyA1 <> propertyC1) in his sample. So your downvote is not merited. –  Fabske May 6 '13 at 22:44
What you say is incorrect though: "you can't convert an object if you don't have any information on this object." Generics and generic constraints are especially good in this aspect. Apart from that, by declaring two interfaces in a different library you gain nothing because you can't instantiate an interface. Plus the OP wants two-way conversion. –  Kenneth May 6 '13 at 22:50
No, it's totally correct. Generics and generics constraints use types, so you need to have a type to work on. In the original scenario you don't have any type, you don't have any information on the object. It's why you added a interface in your code: to retrieve information about the object. If you had enough information, why did you add an interface ? –  Fabske May 6 '13 at 22:55
You don't need information of the object nor the type, you just need information on what contract it exposes. That's why you need the interface. Anyway, your suggested solution does not make sense and that's why I downvoted, I will remove the downvote if you correct the error –  Kenneth May 6 '13 at 22:58
show 2 more comments

Your Answer


