.NET Framework Bookmark and Share   
 index > .NET Base Class Library > Casting problem in .net reflection
 

Casting problem in .net reflection

Hi, i have problem during casting object in reflection mechanism

sample:
public class Square
{
int a,b;
public void getSquare()
{
}

public static SquaregetInstance()
{
return new Square();
}

}

lets assume that this class exist in "Figures" assembly...

then i tried receive instance of square class using reflection

Assembly

obj =Activator.CreateInstance(t, null);
module = Assembly.LoadFrom(@"D:\Figures.dll"); Type t= module.GetType("Square");

MethodInfo info = t.GetMethod("getInstance");

Object result;

result = info.Invoke(obj,null);

Any idea what im doing wrong?
Thanks for help

//Figures.Square squareInstance = (Figures.Square)result; at this line problem occur "InvalidCastException
lukie_cookie
Avoid using LoadFrom as much as possible. When your calling code has a reference to Figures.dll and you open it again with Assembly.LoadFrom, you end up having two Figures.dll loaded in memory. The two are loaded into different contexts (one is the Load context and one is the LoadFrom context with a codebase specified) and because of this the Types are not compatible.

http://blogs.msdn.com/suzcook/archive/2003/05/29/57143.aspx

Your best choice is to stop doing unnecessary reflection. Second best is use typeof(Square).Assembly to get to the assembly. Finally if you absolutely must load an assembly via a path, do it like so:

            AssemblyName asmName = Assemblyname.GetAssemblyName( "D:\\Figures.dll" );
            Assembly asm = Assembly.Load( asmName );


This loads the new assembly into the Load context. Because it's the same context as everything else, it won't actually load your Figures.dll a second time if it's already loaded.
  • Marked As Answer bylukie_cookie Tuesday, September 22, 2009 9:38 PM
  •  
Adam Sills
Avoid using LoadFrom as much as possible. When your calling code has a reference to Figures.dll and you open it again with Assembly.LoadFrom, you end up having two Figures.dll loaded in memory. The two are loaded into different contexts (one is the Load context and one is the LoadFrom context with a codebase specified) and because of this the Types are not compatible.

http://blogs.msdn.com/suzcook/archive/2003/05/29/57143.aspx

Your best choice is to stop doing unnecessary reflection. Second best is use typeof(Square).Assembly to get to the assembly. Finally if you absolutely must load an assembly via a path, do it like so:

            AssemblyName asmName = Assemblyname.GetAssemblyName( "D:\\Figures.dll" );
            Assembly asm = Assembly.Load( asmName );


This loads the new assembly into the Load context. Because it's the same context as everything else, it won't actually load your Figures.dll a second time if it's already loaded.
  • Marked As Answer bylukie_cookie Tuesday, September 22, 2009 9:38 PM
  •  
Adam Sills
thank you, its solved my problem :)
lukie_cookie

You can use google to search for other answers

Custom Search

More Threads

• ACTIVEX WITH VS05
• is there a cross framw in .net 2.0
• Use C# to reset Windows 2003 Server Active Directory User Account Password
• Problems using AppSettingsExpressionBuilder declaratively
• Change Internet Explorer Advance Options Programmatically
• Problems with a program
• Read Data from a Com Port (Check to see which com port I need to use)
• How get path of application folder
• encryption algorithm ,blowfish in .net
• xmlTextReader Value Truncated