Frage

I am using Reflection to avoid explicit casting in deep copying an OFX video plugin state.

object pvalue = parameter.GetType().GetProperty("Value").GetValue(parameter, null);

then later....

 OFXParameter newparameter = parameter on new plugin;
            PropertyInfo pi = newparameter.GetType().GetProperty("Value");
            pi.SetValue(newparameter, pvalue, null);

I successfully avoided casting the parameter (of which there can be an evolving pool of types). Now the problem. Each OFX parameter has a keyframes property containing OFXKeyframes TValue,TKey. I can not find a way to access the list of keyframes without explicit casting.

object keys = parameter.GetType().GetProperty("Keyframes").GetValue(parameter,null);
MethodInfo enumofkeys = keys.GetType().GetMethod("GetEnumerator");
object o = enumofkeys.Invoke(keys,null);

Every response I have read explains generic casting with IEnumerable but I must be missing something. I am restricted to Net 3.5 and with a restriction to 2.0 preferable. I have no access to IEnumerable without an explicit cast. The compiler and intellisense always show IEnumerable as IEnumerable<>.

So with either the object keys or the object o (the enumerator) how can I break them down in Reflection to get access? I have tried invoking the CopyTo method (gained from Reflection) of the Keyframes object but it throws an object not an instance exception (which is proven wrong by an actual explicit cast of the keyframes enumerable.) Once I figure out how to enumerate through the keys object or figure out how to turn the o object in to a functioning enumerator I am confident I can get the rest of the property values I need. The deep copy structure will never have to be updated even if new parameter types are introduced.

War es hilfreich?

Lösung

It would be a lot simpler if you used the relevant interface. In this case, you can use the IEnumerable interface.

IEnumerable keys = parameter.GetType().GetProperty("Keyframes")
    .GetValue(parameter, null) as IEnumerable;

You have to then deal with the type of the enumerated objects, but the enumerable itself is easier to deal with.

IEnumerable is in the System.Collections namespace, not System.Collections.Generic.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top