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. Dynamic Reference With Inheritence

Dynamic Reference With Inheritence

Scheduled Pinned Locked Moved C#
comdata-structurestoolshelpquestion
5 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.
  • A Offline
    A Offline
    Adam R Harris
    wrote on last edited by
    #1

    Hey All, I posted yesterday about dynamic reference and attaching to the AssemblyResolve event. The suggested solution i received is/was great however; that suggestion only seems to function for both Windows and Web Applications and the project i am working on is a Class Library with a few classes that inherit from the 'Dynamic Reference'. I can get it to work if i use a wrapper around my class library but unfortunately this is not going to work for the final product. Here is a sample of what i have working for Windows/Wed Applications; based on the reply from Ennis Ray Lynch, Jr.[^] public class AssemblyResolver { private bool mLoading = false; /// /// Creates a new instance of the AssemblyResolver /// /// The current AppDomain public AssemblyResolver(AppDomain appDomain) { appDomain.AssemblyResolve += new ResolveEventHandler(OnAssemblyResolve); } /// /// Resolves any unfound references /// /// The caller /// The assembly to load /// private Assembly OnAssemblyResolve(object sender, ResolveEventArgs args) { //Prevent Stack overflow, this method will recurse without this line Assembly result = null; if (!mLoading) { try { mLoading = true; if (args.Name.Contains([My Dynamic Reference])) { result = Assembly.LoadFrom(findDir() + "\\[Library].dll"); } }//end try finally { mLoading = false; } }//end mLoading return result; } }
    Usage: AssemblyResolver res = new AssemblyResolver(AppDomain.CurrentDomain); Any help or advice would be greatly appreciated, you can find my original post here[

    E 1 Reply Last reply
    0
    • A Adam R Harris

      Hey All, I posted yesterday about dynamic reference and attaching to the AssemblyResolve event. The suggested solution i received is/was great however; that suggestion only seems to function for both Windows and Web Applications and the project i am working on is a Class Library with a few classes that inherit from the 'Dynamic Reference'. I can get it to work if i use a wrapper around my class library but unfortunately this is not going to work for the final product. Here is a sample of what i have working for Windows/Wed Applications; based on the reply from Ennis Ray Lynch, Jr.[^] public class AssemblyResolver { private bool mLoading = false; /// /// Creates a new instance of the AssemblyResolver /// /// The current AppDomain public AssemblyResolver(AppDomain appDomain) { appDomain.AssemblyResolve += new ResolveEventHandler(OnAssemblyResolve); } /// /// Resolves any unfound references /// /// The caller /// The assembly to load /// private Assembly OnAssemblyResolve(object sender, ResolveEventArgs args) { //Prevent Stack overflow, this method will recurse without this line Assembly result = null; if (!mLoading) { try { mLoading = true; if (args.Name.Contains([My Dynamic Reference])) { result = Assembly.LoadFrom(findDir() + "\\[Library].dll"); } }//end try finally { mLoading = false; } }//end mLoading return result; } }
      Usage: AssemblyResolver res = new AssemblyResolver(AppDomain.CurrentDomain); Any help or advice would be greatly appreciated, you can find my original post here[

      E Offline
      E Offline
      Ennis Ray Lynch Jr
      wrote on last edited by
      #2

      How would you inherit from a dynamic reference? Perhaps, as much as I hate to quote patterns, the wrapper pattern will help you as well as the new dynamic keyword (at the expense of static type checking) http://en.wikipedia.org/wiki/Adapter_pattern[^]

      Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

      A 1 Reply Last reply
      0
      • E Ennis Ray Lynch Jr

        How would you inherit from a dynamic reference? Perhaps, as much as I hate to quote patterns, the wrapper pattern will help you as well as the new dynamic keyword (at the expense of static type checking) http://en.wikipedia.org/wiki/Adapter_pattern[^]

        Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

        A Offline
        A Offline
        Adam R Harris
        wrote on last edited by
        #3

        Thx for the reply Ennis, Let me explain in greater detail for you. I am developing a plug-in for Sage CRM, the plug-in needs to implement/inherit from Sage.CRM.Wrapper.IeWareBlock located in the SageCRMWrapper.dll. At design time i can have a reference to the SageCRMWrapper.dll, but at run-time i would like the reference to the SageCRMWrapper.dll to be dynamic as Sage releases patchs all the time that will not effect my plug-in but cause me to rebuild/re-deploy to all the clients using the plug-in. so here is a sample class; public class myBlock : Sage.CRM.Wrapper.IeWareBlock { public myBlock() { AssemblyResolver res = new AssemblyResolver(AppDomain.CurrentDomain); } // implementation of the interface }
        obviously this solution fails, because when the framework goes to create this class it fails because the constructor has not been called which is where i wire the AssemblyResolve event. Ofcourse if there is a change to the Interface i would need to rebuild to implement the changes to the interface. I'm not sure how using dynamics will help out here, but im really not that familiar with them so it is very likely i am wrong. Thanks again for the reply.

        Don't comment your code - it was hard to write, it should be hard to read!

        E 1 Reply Last reply
        0
        • A Adam R Harris

          Thx for the reply Ennis, Let me explain in greater detail for you. I am developing a plug-in for Sage CRM, the plug-in needs to implement/inherit from Sage.CRM.Wrapper.IeWareBlock located in the SageCRMWrapper.dll. At design time i can have a reference to the SageCRMWrapper.dll, but at run-time i would like the reference to the SageCRMWrapper.dll to be dynamic as Sage releases patchs all the time that will not effect my plug-in but cause me to rebuild/re-deploy to all the clients using the plug-in. so here is a sample class; public class myBlock : Sage.CRM.Wrapper.IeWareBlock { public myBlock() { AssemblyResolver res = new AssemblyResolver(AppDomain.CurrentDomain); } // implementation of the interface }
          obviously this solution fails, because when the framework goes to create this class it fails because the constructor has not been called which is where i wire the AssemblyResolve event. Ofcourse if there is a change to the Interface i would need to rebuild to implement the changes to the interface. I'm not sure how using dynamics will help out here, but im really not that familiar with them so it is very likely i am wrong. Thanks again for the reply.

          Don't comment your code - it was hard to write, it should be hard to read!

          E Offline
          E Offline
          Ennis Ray Lynch Jr
          wrote on last edited by
          #4

          I think the problem is: AssemblyResolver res = new AssemblyResolver(AppDomain.CurrentDomain); needs to be called when your application starts not when your module is loaded. That is why the AssemblyResolved isn't working. When the constructor for myBlock is called the .NET runtime tries to load the assembly before getting to your constructor. AFAIK you have to bind the Assembly event before the run time tries to resolve the assembly.

          Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

          A 1 Reply Last reply
          0
          • E Ennis Ray Lynch Jr

            I think the problem is: AssemblyResolver res = new AssemblyResolver(AppDomain.CurrentDomain); needs to be called when your application starts not when your module is loaded. That is why the AssemblyResolved isn't working. When the constructor for myBlock is called the .NET runtime tries to load the assembly before getting to your constructor. AFAIK you have to bind the Assembly event before the run time tries to resolve the assembly.

            Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. I also do Android Programming as I find it a refreshing break from the MS. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost

            A Offline
            A Offline
            Adam R Harris
            wrote on last edited by
            #5

            ;) Which is my problem, this class will be instatiated by a Classic ASP Website (Sage CRM) which i cannot control so i cannot modify it to wire to the AssemblyResolve event ...

            Don't comment your code - it was hard to write, it should be hard to read!

            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