Loading assembly dynamically and using it

  • Thread starter Thread starter KK
  • Start date Start date
K

KK

Hi all
Can anybody help me what's going wrong here and how to correct it.
Code is as follows....

System::Object * GetObjectFromAssembly()
{
Assembly * pMyAssembly = Assembly::Load("MyAssembly");
Type * pMyType = pMyAssembly->GetType("MyNameSpace.MyClass");
System::Object * pRet = pMyType->CreateInstance();
pMyAssembly = 0;
return pRet;
}

Here i'm getting the error as "pRet is disposed." How to create object from
Assembly at runtime and use it?

Thanks and Regards
Krishna
 
Krishna,

The following sample is from a VB.NET project I just deployed.
Unfortunately it is not in C++ but perhaps you can glean some ideas.
IReport is an abstract interface I defined that is known by the the caller
and is implemented by the dynamically loaded assembly.


Friend Class ReportProxy
Friend ReportAssembly As String
Friend ReportNamespace As String

Public Function Report() As IReport
Dim hdl As System.Runtime.Remoting.ObjectHandle
hdl = Activator.CreateInstance(ReportAssembly, ReportNamespace)
Return CType(hdl.Unwrap, MyCom.General.Reporting.IReport)
End Function
End Class
 
-----Original Message-----
Hi all
Can anybody help me what's going wrong here and how to correct it.
Code is as follows....

System::Object * GetObjectFromAssembly()
{
Assembly * pMyAssembly = Assembly::Load ("MyAssembly");
Type * pMyType = pMyAssembly->GetType ("MyNameSpace.MyClass");
System::Object * pRet = pMyType->CreateInstance();
pMyAssembly = 0;
return pRet;
}

Here i'm getting the error as "pRet is disposed." How to create object from
Assembly at runtime and use it?

Thanks and Regards
Krishna


.
Hey are there any overloads of the CreateInstance that
take void as a parameter, I didn't see one in the
documentations? Also is CreateInstance returning a new
Object or just a plain pointer? I remember getting a
similiar error like yours awhile back. I skimmed my code
and realized that the returning function only returned a
plain pointer not a new one. I saw your post and thought
maybe you did the same mistake as me. Hope this maybe
helps!!
 
Back
Top