Dynamic Reference With Inheritence
-
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[ -
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[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
-
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
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!
-
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!
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
-
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
;) 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!