I need to be able to dynamically instantiate an object using reflection. I have a PropertyInfo at this point and need to get the Assembly of this object before I can call the CreateInstance method.

The code looks like this:

Dim subObjectPropInfo As PropertyInfo = GetPropertyInfo(baseObject, "Name")
Dim asm As Assembly = ?????
Dim subObjType As Type = asm.GetType(subObjectPropInfo.PropertyType.FullName)
Dim subObject As Object = Activator.CreateInstance(subObjType)

The question is: How can I get the assembly for the object I need to instantiate from a PropertyInfoobject ? Is it possible ? If not, what is the way to do it ?

I use to have always the same assembly so the way to do it was not really dynamic. Now that objects come from multiple assemblies, it cannot be done the way it used to be.

有帮助吗?

解决方案

Try using the PropertyType.Assembly properties from the PropertyInfo object.

subObjectPropInfo.PropertyType.Assembly
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top