Create dll and use it on runtime
-
Hello, I have a compiled dll that contain class "MyClasses.dll". One of the class is called "class1". In runtime i need to compile a code that used "class1" and i need to used to compile code as dll in the runtime code. How can i do it? Example: 1. MyClasses.dll contain class1. class1 look like this:
delegate double GetRank(class1 c1);
public class1
{
public double Num1;
public double Num2;
public event GetMyRank;
}2. I want to get a string and build a function in rumtime for sorting. for example the user can write n Textbox "Num1*Num2" and then i want to create this function:
public double GetRank1(class1 c1)
{
return c1.Num1*c1.Num2;
}3. Compile and Create dll that contain this function.(GetRank1) 4. In runtime load the dll for exmple "MyDll.dll" 5. Initialize each instance of class1 with the event "GetRank1" for "GetMyRank" event. :
class1 c1 = new class1();
...
..
c1.GetMyRank= new GetRank(MyDll.GetRank1);
....6. And then i want to compare 2 instance of 'class1' by this function. I Allready try to use CodeDomProvider and CompilerParameters but i need the use the dll "MyClasses.dll" in the CompilerParameters and it does not recognize him:
ErrorText: "The type or namespace name 'MyClasses.dll' could not be found (are you missing a using directive or an assembly reference?)"
The second problem is how to use to compiled code in my code at runtime. for example: Assembly a = Assembly.GetAssembly(Type.GetType("GetRank1Class")); and then try to call the function GetRank1. How can i solve those problems? -
Hello, I have a compiled dll that contain class "MyClasses.dll". One of the class is called "class1". In runtime i need to compile a code that used "class1" and i need to used to compile code as dll in the runtime code. How can i do it? Example: 1. MyClasses.dll contain class1. class1 look like this:
delegate double GetRank(class1 c1);
public class1
{
public double Num1;
public double Num2;
public event GetMyRank;
}2. I want to get a string and build a function in rumtime for sorting. for example the user can write n Textbox "Num1*Num2" and then i want to create this function:
public double GetRank1(class1 c1)
{
return c1.Num1*c1.Num2;
}3. Compile and Create dll that contain this function.(GetRank1) 4. In runtime load the dll for exmple "MyDll.dll" 5. Initialize each instance of class1 with the event "GetRank1" for "GetMyRank" event. :
class1 c1 = new class1();
...
..
c1.GetMyRank= new GetRank(MyDll.GetRank1);
....6. And then i want to compare 2 instance of 'class1' by this function. I Allready try to use CodeDomProvider and CompilerParameters but i need the use the dll "MyClasses.dll" in the CompilerParameters and it does not recognize him:
ErrorText: "The type or namespace name 'MyClasses.dll' could not be found (are you missing a using directive or an assembly reference?)"
The second problem is how to use to compiled code in my code at runtime. for example: Assembly a = Assembly.GetAssembly(Type.GetType("GetRank1Class")); and then try to call the function GetRank1. How can i solve those problems?My experience with this stuff is limited, from what I've done in the past I would say: 1. you need to add references to all the managed DLL files you need at compile time, hence use
CompilerParameters.ReferencedAssemblies.Add(dllFileName)
2. you can't use dynamic classes and methods as usual; you need reflection code for every instance and method, such asAssembly.CreateInstance()
andType.GetMember()
andMethodInfo.Invoke()
; all of which is cumbersome. Are you sure you need it? You didn't tell much about the real functionality of your app. FWIW: I expect you can reduce the hassle by having a fixed interface that works like a contract for your dynamic class; once you get an instance (still through reflection), you should be able to invoke methods in the normal way as long as they are part of that interface. :)Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!