Situation: The baseType type is known only at runtime. objectInstance - this is a type of type baseType objectInstance was obtained from a call to a dynamic method
It is required:
Type baseType = ...; // obtained at runtime var baseDynamicInstance = (basetype) objectInstance; // or reflection cast
when it is encoded, it works
var oi = (PartnerBase) objectInstance;
tried:
public object CastPocoInstance(Type targetType, object objectInstance) { MethodInfo castMethod = objectInstance.GetType().GetMethod("Cast").MakeGenericMethod(targetType);
Error: null object ref error.
in the next window, I see objectInstance.GetType (). GetMethod ("Cast") returns null
objectInstance.GetType.GetMethods () // shows the list in the immediate window. // casting method not shown
I have looked at many examples. This tells me that Type.GetMethod ("Cast") was right. But that does not work. Itβs clear that Iβm doing something wrong.
Any advice
EDIT: Call failed without casting down to a base hard-set
[Microsoft.CSharp.RuntimeBinder.RuntimeBinderException] = {"The best overloaded method match for 'P42.RepositoryBase.GetEntityState (P42.Core.PartnerBase)' has some invalid arguments"}
EDIT2: AN ObjectInstance is retrieved from a call to a dynamic method. The object must be used when invoking a dynamic method. If it's hard for me to write code, it works. var x = (base object) ObjInstance And call the dynamic method using x. it works.
The base type is also known only at run time. Is there a way to dynamically allocate a SpecificObject for a BAseObject?
source share