{"id":556,"date":"2004-01-14T21:47:02","date_gmt":"2004-01-15T01:47:02","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=556"},"modified":"2004-01-14T21:47:02","modified_gmt":"2004-01-15T01:47:02","slug":"net-reflection-and-multiple-assemblies","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2004\/01\/14\/net-reflection-and-multiple-assemblies\/","title":{"rendered":".NET, Reflection and multiple assemblies"},"content":{"rendered":"<p>As part of a project I worked on at <a href=\"http:\/\/www.mindseye.com\/\">Mindseye<\/a>, I wrote a couple utilities using C# that used reflection to query a given type for the existence of a method and if the method existed, to then invoke the method.  In short, the utility allowed me to do this (pseudo-code):<br \/>\n<code><br \/>\nAbstractType type = AbstractTypeFactory.Find(\"com.company.SomeClass\");<br \/>\nstring result = type.InvokeMethod(\"delete\");<br \/>\n<\/code><br \/>\nThe AbstractTypeFactory type has a single static method &#8216;Find()&#8217; that looks for the given type by name.  If successful, it returns an instance of AbstractType, which you can use to invoke a given method on the type you looked up using the InvokeMethod() method.  The InvokeMethod() method checks to see if the given method is a static or instance method, creates an instance if necessary, and then calls the method.  <\/p>\n<p>These utilities worked fine until I need to call the Find() method against a type that wasn&#8217;t in the same assembly as the assembly that my utility was in.  Inside the AbStractTypeFactory (which is a pseudo-name by the way), I used the .<a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cpref\/html\/frlrfsystemtypeclasstopic.asp?frame=true\">NET Type class<\/a>:<br \/>\n<code><br \/>\n\/\/ Gets the Type with the specified name, performing a case-sensitive search.<br \/>\nType type = Type.GetType(\"com.company.SomeClass\");<br \/>\n<\/code><br \/>\nI didn&#8217;t have time to look into why the GetType() method wouldn&#8217;t find types outside of the currently assembly&#8230; until today when I really needed to be use the utilities against a different assembly.  It turns out that it&#8217;s relatively simple to do. Instead of using the type name (ie: &#8220;com.company.SomeClass&#8221;), you can use the <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cpref\/html\/frlrfsystemtypeclassassemblyqualifiednametopic.asp\">AssemblyQualifiedName<\/a>, which looks likes this:<br \/>\n<code><br \/>\ncom.company.SomeClass, assemblyname, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null<br \/>\n<\/code><br \/>\nwhere assemblyname is the name of the DLL that com.company.SomeClass exists in. It&#8217;s relatively easy to retrieve the AssemblyQualifiedName using the AssemblyQualifiedName property of the Type type. For example, the .NET documentation gives this example:<br \/>\n<code><br \/>\nType objType = typeof(System.Array);<br \/>\nConsole.WriteLine (\"Qualified assembly name: {0}.\", objType.AssemblyQualifiedName.ToString());<br \/>\n<\/code><br \/>\nThere are alot of good reasons why Microsoft designed the GetType() method the way they did, the majority of them related to <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cpguide\/html\/cpconhowruntimelocatesassemblies.asp\">how the runtime locates assemblies<\/a>, which you should read about when you have a alot of time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>As part of a project I worked on at Mindseye, I wrote a couple utilities using C# that used reflection to query a given type for the existence of a method and if the method existed, to then invoke the method. In short, the utility allowed me to do this (pseudo-code): AbstractType type = AbstractTypeFactory.Find(&#8220;com.company.SomeClass&#8221;); &hellip; <a href=\"https:\/\/cephas.net\/blog\/2004\/01\/14\/net-reflection-and-multiple-assemblies\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">.NET, Reflection and multiple assemblies<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[17],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/556"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=556"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/556\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=556"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=556"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=556"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}