mercredi 3 octobre 2018

Copying reflected generic type value

Alright, so I have a generic copy method.

In my class I have a class called GenericPrimitiveContainer - which just holds T Value.

In my generic copy method, I want to copy Value from instance A to instance B.

So far it looks like this:

var instance = Activator.CreateInstance(props[i].PropertyType);
var container = props[i].GetValue(source, null);

These give me the new instance (instance) and the one I'm copying from (container).

I can say something like this:

(instance as GenericPrimitiveContainer<int>).Value = (container as GenericPrimitiveContainer<int>).Value;

But that's not very generic. If I remove the 'int', then it tells me 'type expected', and if I remove the angular brackets it won't work either.

I know the type I want, and that type is in a Type[] called genericArgs. But if I put in there, it also won't work.

How can I do this?





Aucun commentaire:

Enregistrer un commentaire