Cast object to another type at run time

Taz Mania
void A(object o)
{
    ...
    Assembly a = Assembly.LoadFile(...);
    Type t = a.GetType(@"namespace.className");
    MethodInfo mi = t.GetMethod(@"MethodName");
    mi.Invoke(instace, new Object[] {o});
    ....         
}

the method I need to invoke accept another type, I need to cast the object to that type, but, the type is known only at run time, so can I convert/cast object to another type known only at run time ? I can change only method A.

thank you.

usr

The Reflection APIs only deal with object references. Casting a reference is an operation on that reference. It does not affect the object in any way.

For that reason it is not necessary to cast anything here. Just pass o to Invoke like you are already doing. The Reflection API validates the type of o and passes it to the method you want to call.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

Typescript - clone an object and cast into another type

From Dev

Cast one object to another

From Dev

Determine type of Object and cast as type

From Dev

Cast a run-time Dictionary<K, V> that comes as a generic type argument into its proper compile-time type

From Dev

Unable to cast object of type 'WhereSelectEnumerableIterator

From Dev

Cast object type in a LINQ statement

From Dev

Unable to cast object of type exception

From Dev

Working with Time Type in Fluent Nhibernate generates exception "Unable to cast object of type 'System.DateTime' to type 'NHibernate.Type.TimeType"

From Dev

Cast object to generic type at runtime

From Dev

Cast object (type double) to int

From Dev

Cast List<Object> to unknown type

From Dev

Trying to cast one object type into another in Python

From Dev

C# cast object to another type stored in string variable

From Dev

run time error when Casting (down casting) a type to another subType

From Dev

Cast object to method generic type

From Dev

How to cast one object into another

From Dev

Cast one object to another

From Dev

Unable to cast object of type 'SingleResult'

From Dev

Cast object to generic type at runtime

From Dev

Cast List<Object> to unknown type

From Dev

How to cast a object into another?

From Dev

Cast an object to a data type?

From Dev

Unable to cast object of type,

From Dev

Cast object to unknown type

From Dev

C# cast object to another type stored in string variable

From Dev

Invalid cast exception on using virtual method with base class being the run time type c#

From Dev

Determine object type at run time - C#

From Dev

Scala Cast object to another type

From Dev

Dotvvm run-time binding to 'object' type

Related Related

HotTag

Archive