Hi,
i used your code by example to make working the following personal code and it works fine:
string dll = "DigitalContentLibrary.dll";
string namespaceClass = "DigitalContentLibrary.Manager";
string metodName = "BuyDigitalContent";
object[] param = { "660305", "200", 1.00, 0, 0, "IT" };
Assembly asm = Assembly.LoadFrom(dll);
Type type = asm.GetType(namespaceClass);
object obj = Activator.CreateInstance(type);
Response ret = (Response)type.InvokeMember(metodName, BindingFlags.InvokeMethod |
BindingFlags.Instance | BindingFlags.Public, null, obj, param);
return ret;
//
// Response object is a public class with 6 parameter
// DigitalContentLibrary is the namespace
// Manager is the class that contains the metod BuyDigitalContent
//
Aimslife wrote:
Type casting problem while using Reflection
22-Feb-10
Hi
I am newbie on CSharp. I am using Reflection for run-time functio
calling. Everything is working perfect, but problem is coming while sel
defined Abstract Data Type casting. I am made two project in on
solution, (1) Class Library Type, (2) Console Type project
Class Library Project has two classes
Examin.cs fil
namespace ExampleLi
public class Exami
private Result result = new Result()
public void setResultValue(int val
result.Sum = val
public Result getResultObj(
return result
public int getResultValue(
return result.Sum
Result.cs fil
namespace ExampleLi
public class Resul
private int sum
public int Su
get { return sum;
set { sum = value;
public int getSumValue(
return sum
I added Class Library Reference in Console Project and wrote give cod
for calling "getResultObj" function
string loadLib = null
Object value = null
string sAssemblyName = args[0]
Assembly assem = Assembly.LoadFrom(sAssemblyName)
loadLib = "ExampleLib.Examin"
Type exType = assem.GetType(loadLib)
Object exInstance = Activator.CreateInstance(exType)
ExampleLib.Result rest = (Result)exType.InvokeMember("getResultObj"
BindingFlags.InvokeMethod
BindingFlags.Instance | BindingFlags.Public
null, exInstance, null)
Run time exception is coming in debug mode as given below
[A]ExampleLib.Result cannot be cast to
ExampleLib.Result. Type
originates from 'ExampleLib, Version=1.0.0.0, Culture=neutral
PublicKeyToken=null' in the context 'LoadFrom' at location 'D:\4
Development\Reflaction\ExampleLib\ExampleLib\bin\Debug\ExampleLib.dll'
Type B originates from 'ExampleLib, Version=1.0.0.0, Culture=neutral
PublicKeyToken=null' in the context 'Default' at location 'D:\4
Development\Reflaction\ExampleLib\TestingConsole\bin\Debug\ExampleLib.dll'
Please guide me, what I am doing wrong? and how to fix it
Regards
-Aimslife
Previous Posts In This Thread:
Type casting problem while using Reflection
Hi
I am newbie on CSharp. I am using Reflection for run-time functio
calling. Everything is working perfect, but problem is coming while sel
defined Abstract Data Type casting. I am made two project in on
solution, (1) Class Library Type, (2) Console Type project
Class Library Project has two classes
Examin.cs fil
namespace ExampleLi
public class Exami
private Result result = new Result()
public void setResultValue(int val
result.Sum = val
public Result getResultObj(
return result
public int getResultValue(
return result.Sum
Result.cs fil
namespace ExampleLi
public class Resul
private int sum
public int Su
get { return sum;
set { sum = value;
public int getSumValue(
return sum
I added Class Library Reference in Console Project and wrote give cod
for calling "getResultObj" function
string loadLib = null
Object value = null
string sAssemblyName = args[0]
Assembly assem = Assembly.LoadFrom(sAssemblyName)
loadLib = "ExampleLib.Examin"
Type exType = assem.GetType(loadLib)
Object exInstance = Activator.CreateInstance(exType)
ExampleLib.Result rest = (Result)exType.InvokeMember("getResultObj",
BindingFlags.InvokeMethod |
BindingFlags.Instance | BindingFlags.Public,
null, exInstance, null);
Run time exception is coming in debug mode as given below,
[A]ExampleLib.Result cannot be cast to ExampleLib.Result. Type A
originates from 'ExampleLib, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null' in the context 'LoadFrom' at location 'D:\4.
Development\Reflaction\ExampleLib\ExampleLib\bin\Debug\ExampleLib.dll'.
Type B originates from 'ExampleLib, Version=1.0.0.0, Culture=neutral,
PublicKeyToken=null' in the context 'Default' at location 'D:\4.
Development\Reflaction\ExampleLib\TestingConsole\bin\Debug\ExampleLib.dll'.
Please guide me, what I am doing wrong? and how to fix it?
Regards,
-Aimslife
Please also guide me, why compiler is giving "InvalidCastException"?
Please also guide me, why compiler is giving "InvalidCastException"?
On 22-Feb-2010 3:57 PM, Aimslife wrote:
Aimslife wrote:I do not know what you are passing as args[0] of the
Aimslife wrote:
I do not know what you are passing as args[0] of the commandline parameter
, but my guess would be that it is the complete path to the buildtarget
of the class library project (i.e. "...\classlibrary\debug\bin\...").
Since you already referenced that library in the console project, the
..NET runtime now has two versions of that dll loaded into different
contexts: One is loaded as part of the console application, the other
one a different copy, loaded from a location on disk.
Assuming this is just an exercise for getting familiar with reflection,
the solution would be to pass the name of the assembly without any path
qualification, i.e. Assembly.LoadFrom("Library.dll").
The biggest question is: What do you want to achieve?. You already
*have* a reference to that library, why go loading it again, jump
through lots of hoops, to achieve something that can be done without
reflection.
--
Willem van Rumpt
Hi,Please take look on following lines, in exception details.
Hi,
Please take look on following lines, in exception details.
Type A originates from
'D:\4.Development\Reflaction\ExampleLib\ExampleLib\bin\Debug\ExampleLib.dll'.
Type B originates from
'D:\4.Development\Reflaction\ExampleLib\TestingConsole\bin\Debug\ExampleLib.dll'.
When you run TestingConsole application, referenced assembly ExampleLib is
copied to TestingConsole\bin\Debug\ folder, and loaded on execution of
process as A. but argument in Assembly.LoadFrom(argument) is the build path
of ExampleLib, the assembly using this statement is loaded into different
memory space, with different signature as B, because of different path.
that is why you are getting typecast error.
Hope this Helps.
--
Regards,
Guramrit Singh
S/W Developer
LOGIC ERP Solutions Pvt. Ltd.
Hello,I would strongly suggest using an interface :http://www.informit.
Hello,
I would strongly suggest using an interface :
http://www.informit.com/articles/article.aspx?p=332874
This way once the instance is created, you will be able to use it without
using Reflection (which should be a last resort).
For now the problem seems to be a file management issue. You are referencing
two distinct files (could it be the same binary ?) i.e. this is not the same
type.
If this is for a plugin system, you could have a single DLL referenced for
both the host and the plug in, to provide common types. The plug in is
loaded and access throught the type and interface described here.
You also have a whole plug in API that is perhaps overkill to begin with but
that you may want to check
(http://msdn.microsoft.com/en-us/library/system.addin.aspx). For example it
provides also the ability to control the security context in which the plug
in code runs (i.e. my understanding is that you could create this way a plug
in that is not allowed to write to disk even if your main app is allowed to
do so)....
--
Patrice
Hi Willem,args[0] = ..\..\..\ExampleLib\bin\Debug\ExampleLib.
Hi Willem,
args[0] = ..\..\..\ExampleLib\bin\Debug\ExampleLib.dll
Yes, it is an exercise to resolve all basic issues regarding project
implementation. Actually, I want to develop dynamic implementation of
web-services client will XML file. It will having Complete Qualified
Object Name, Function names to be called and parameter values if any.
All required information will communicate to web service to make
operation according to given information. I have some Business Object
which will be present on both side as I have taken in example,
Examin.cs file
namespace ExampleLib
{
public class Examin
{
private Result result = new Result();
public void setResultValue(int val)
{
result.Sum = val;
}
public Result getResultObj()
{
return result;
}
public int getResultValue()
{
return result.Sum;
}
}
}
Result.cs file
namespace ExampleLib
{
public class Result
{
private int sum;
public int Sum
{
get { return sum; }
set { sum = value; }
}
public int getSumValue()
{
return sum;
}
}
}
Problem is coming while calling ExampleLib.Examin.getResultObj() with
Reflection and got InvalidCastingException. where as, I do not
understand whats wrong with code. Please guide me, how I can fix it.
Regards,
Aimslife
..
Thanks Guramrit!
Thanks Guramrit! Problem has been resolved.
On 22-Feb-2010 5:29 PM, Guramrit Singh wrote:
Aimslife wrote:I am not sure I understand what you want to develop.
Aimslife wrote:
I am not sure I understand what you want to develop. It sounds like you
want to develop some remoting framework, but invent it all from the
ground up. Maybe you can have a peek at .NET remoting
(http://msdn.microsoft.com/en-us/library/kwdt6w2k(VS.71).aspx) or WCF
(http://msdn.microsoft.com/en-us/library/ms735119.aspx) to see if that
does not already provide what you want.
You get the exception because in your case, you end up having the same
assembly loaded twice in different contexts. The first one is loaded in
the context of the console application, the other one is loaded from
disk in (as the exception details say) the "LoadFrom" context. The
assemblies and the types in them are not interchangeable. The .NET
runtime treats them as distinct types.
see http://msdn.microsoft.com/en-us/library/dd153782.aspx for both a
better and more in-depth explanation of assemblies & contexts.
--
Willem van Rumpt
Submitted via EggHeadCafe - Software Developer Portal of Choice
Excel Identifying which formulas are slowing down workbook recalaculation
http://www.eggheadcafe.com/tutorial...are-slowing-down-workbook-recalaculation.aspx