-
Calling an assembly
[CODE]
Code:
namespace ClassLibraryB
{
public class Class1
{
public int result { get; set; }
public static int i { get; set; }
public static int j { get; set; }
public void Add(int ii, int jj)
{
i = ii;
j = jj;
result = i + j;
}
public void Add2()
{
result = i + j;
}
}
}
This gets called statically and gives me an answer
Code:
ClassLibraryB.Class1 objB = new ClassLibraryB.Class1();
objB.Add(4, 16);
objB.Add2();
kk = objB.result;
textBox1.Text += "Addition =" + kk.ToString() + "\r\n";
However when I try to call the dll using below it fails with since it is not static
Code:
Assembly testAssembly = Assembly.LoadFile(strDLL);
Type calcType = testAssembly.GetType("ClassLibraryB.Class1");
object calcInstance = Activator.CreateInstance(calcType);
PropertyInfo numberPropertyInfo = calcType.GetProperty("i");
numberPropertyInfo.SetValue(calcInstance, 5, null);
PropertyInfo numberPropertyInfo2 = calcType.GetProperty("j");
numberPropertyInfo2.SetValue(calcInstance, 15, null);
int value2 = (int)numberPropertyInfo.GetValue(calcInstance, null);
int value3 = (int)numberPropertyInfo2.GetValue(calcInstance, null);
calcType.InvokeMember("Add2",BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.Public,
null, null, null);
PropertyInfo numberPropertyInfo3 = calcType.GetProperty("result");
int value4 = (int)numberPropertyInfo3.GetValue(calcInstance, null);
it does not like calling Add2 as a non static.
Non-static method requires a target.
Any ideas to be able to call the same dll func from both static and dynamic way?
Thanks
-
Re: Calling an assembly
A method can't be both static and be an instance method.