Hi guys
(please refer to this thread for more info on the purpose of this exercise)
http://forum.unity3d.com/threads/92103-large-number-of-math-formulas?p=596761#post596761
Ok, so I’m trying to run my first prototype dynamic dll (dynamic type/class) at runtime and it is created successfully. But how’s this…When I try to call this dll from within my project, it gives me a compile error, because obviously I’m calling a class that doesn’t exist at design time? The result is that I get a compile error that says the reference does not exist. What would a workaround for this be?
Edit: Ok, so I suppose I must use delegates for this? Anybody that can give me an example?
Thanks
You can’t reference something that doesn’t exist - so yeah, go back to your books and rethink how that’s going to work.
What NPSF3000 said is true, but could you post some code about how you want to access the class and how the class will be created at runtime, how the structures look like, eg.? Maybe there is a solution for this.
Also the exact error message would be fine.
Ok, here is the code that compiles the new dynamic type (class/assembly or whatever)
AssemblyName myAssemblyName = new AssemblyName();
myAssemblyName.Name = "TestAssembly";
AssemblyBuilder myAssembly = Thread.GetDomain().DefineDynamicAssembly(myAssemblyName, AssemblyBuilderAccess.RunAndSave);
ModuleBuilder myModule = myAssembly.DefineDynamicModule(myAssemblyName.Name, myAssemblyName.Name + ".dll", true);
TypeBuilder myTypeBuilder = myModule.DefineType("TestClass", TypeAttributes.Public);
MethodBuilder myMethod = myTypeBuilder.DefineMethod("TestFunction", MethodAttributes.Public | MethodAttributes.Static, typeof(String), new Type[] { typeof(int) });
ILGenerator myMethodIL = myMethod.GetILGenerator();
LocalBuilder myLB1 = myMethodIL.DeclareLocal(typeof(string));
myLB1.SetLocalSymInfo("myString");
LocalBuilder myLB2 = myMethodIL.DeclareLocal(typeof(int));
myLB2.SetLocalSymInfo("myInt", 1, 2);
myMethodIL.Emit(OpCodes.Ldarg_0);
myMethodIL.Emit(OpCodes.Stloc_1);
myMethodIL.Emit(OpCodes.Ldstr, "string value");
myMethodIL.Emit(OpCodes.Stloc_0);
myMethodIL.Emit(OpCodes.Ldloc_0);
myMethodIL.Emit(OpCodes.Ret);
Type myType1 = myTypeBuilder.CreateType();
myAssembly.Save(myAssemblyName.Name + ".dll");
and it works well and gives me this (FYI: I have extracted the c# from the dll using reflector):
public class TestClass
{
// Methods
public static string TestFunction(int num1)
{
int num = num1;
return "string value";
}
}
Then I try to call this class (pretty much using msdn’s example) by doing this:
string myObject2 = myType1.InvokeMember("TestFunction", BindingFlags.InvokeMethod, null, null, new object[] { 42 });
and the error says “cannot implicitly convert type object to string”. So I have tried all sorts of different types, including Object, but then it complains that Object is ambiguous with Unity Object
I’m also looking into NPSF3000’s suggestion of using linq expression trees, but that also comes with it’s share of headaches. This is all new for me, but I have to do it in the current project I’m working on, so I just have to power through!
Well simply cast the result to string 
string myObject2 = **(string)**myType1.InvokeMember(“TestFunction”, BindingFlags.InvokeMethod, null, null, new object[ ] { 42 });
This post is so backwards.
You’re creating dynamic types but overlooked casting the System.Object to System.String??
DOES NOT COMPUTE.
Thanks marrrk
and sorry andorov for making such a rookie mistake, hope that makes you feel better!