Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Create dll and use it on runtime

Create dll and use it on runtime

Scheduled Pinned Locked Moved C#
tutorialquestionalgorithmshelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • B Offline
    B Offline
    bonzaiholding
    wrote on last edited by
    #1

    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?

    L 1 Reply Last reply
    0
    • B bonzaiholding

      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?

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      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 as Assembly.CreateInstance() and Type.GetMember() and MethodInfo.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!


      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups