-----------------------------------------------------------

New Message on BDOTNET

-----------------------------------------------------------
From: Sitaraman
Message 13 in Discussion

Hi   Late Binding :) eh...  I should have made the sample in vb with the option strict 
and option explicit set to "on".  Then it would have given me the error in vb also.  
So u want it in C#?   Here u go.  I have ported the two functions which do the 
"Version Get" and the "Library Path Get"to C#  and is as follows      public String 
fnGetTypeVersionNumberFromAnyOneTypeInAssembly(System.Reflection.Assembly 
p_objAssembly, Boolean p_blnGetCOMLibraryPathAlso)                      {              
                 Type[] l_objtypes;                             Type l_objType;        
                     Object l_objObject;                             l_objtypes = 
p_objAssembly.GetTypes();                             String l_strTypeVersion="";      
                       String l_strVersion="";                             String 
l_strCOMLibraryPath="";                             int l_intTypeCtr=0;                
             int l_intTotalTypes=l_objtypes.Length;                             
for(l_intTypeCtr=0;(l_intTypeCtr<l_intTotalTypes);l_intTypeCtr++)                      
       {                                    try                                    {   
                                        
l_objType=(Type)l_objtypes.GetValue(l_intTypeCtr);                                     
      l_objObject = p_objAssembly.CreateInstance(l_objType.ToString());                
                           // This is a overkill. here im filtering in order to get a 
property(first Parameter                                           // to the function 
is the Name of the Property,  which in our case is "Version") which                    
                         // could be Static or Instance Based, Public or 
Private(private is irrelevant, as it                                            // 
cannot anyways be accessed.  Modify the bindingflags according to your convenience     
                                      // Ignore Case Flag is used to make the search 
for the property case-insensitive.  So u                                            // 
can even pass "VeRsIoN" as the first parameter :)                                      
     l_strVersion = (String)l_objObject.GetType().InvokeMember(                        
                          "Version",BindingFlags.IgnoreCase|                           
                        BindingFlags.GetProperty|BindingFlags.NonPublic|               
                                   BindingFlags.Public|BindingFlags.Static|            
                                      BindingFlags.Instance ,null,l_objObject,null);   
                                        if (l_strVersion != null && 
l_strVersion.Trim() != "")                                           {                 
                                 l_strTypeVersion = l_strVersion;                      
                            if(p_blnGetCOMLibraryPathAlso==true)                       
                           {                                                         
GuidAttribute l_objGuidAttribute=new GuidAttribute(null);                              
                           Object[] l_objCustomAttributes = 
l_objObject.GetType().GetCustomAttributes(l_objGuidAttribute.GetType(), true);         
                                                GuidAttribute l_objGUIDAttribute = 
(GuidAttribute)l_objCustomAttributes[0];                                               
          l_strCOMLibraryPath = 
fnGetCOMLibraryPathFromRegistryBasedOnProgId(l_objGUIDAttribute.Value);                
                                  }                                           }        
                            }                                    catch(Exception 
p_objException)                                    {                                   
        // If the Class is not instantiable or if the version property is not 
supported                                           // this exception block is 
reached. Ignore and continue with next type                                           
Console.WriteLine("Error : " + p_objException.Message);                                
    }                                    finally                                    {  
                                         l_objObject=null;                             
       }                             }                             return "Type 
Version Information : " + l_strTypeVersion +                                     "\n" 
+ "COM Library Path : " + l_strCOMLibraryPath;                      }                  
        public String fnGetCOMLibraryPathFromRegistryBasedOnProgId(String p_strClsId)  
                     {                             RegistryKey l_objMainRegistryKey;   
                          //RegistryKey l_objProgIdkey;                             
RegistryKey l_objInprocServer_LocalServer32key;                             String 
l_strClsid = "{" + p_strClsId + "}";                             String 
l_strCOMLibraryPath="";                             try                             {  
                                  l_objMainRegistryKey = Registry.ClassesRoot;         
                           l_objInprocServer_LocalServer32key = 
l_objMainRegistryKey.OpenSubKey("CLSID\\" + l_strClsid + "\\InProcServer32");          
                          if(l_objInprocServer_LocalServer32key ==null)                
                     {                                           
l_objInprocServer_LocalServer32key = l_objMainRegistryKey.OpenSubKey("CLSID\\" + 
l_strClsid + "\\LocalServer32");                                                  
if(l_objInprocServer_LocalServer32key ==null)                                          
        {                                                         return "";           
                                       }                                    }          
                          l_strCOMLibraryPath = 
(String)(l_objInprocServer_LocalServer32key.GetValue(l_objInprocServer_LocalServer32key.GetValueNames()[0]));
                                           return l_strCOMLibraryPath;                 
            }                             catch(Exception p_objException)              
               {                                    Console.WriteLine("Error : " + 
p_objException.Message);                             }                             
return "";                      }                              Have tested it and is 
working fine. Also attaching  the complete source code(have deleted bin/obj.  So u 
need to Rebuild all9.  Now the solution has two projects.  One is the VBClient Form 
with the two functions also existing in VB.  The other project is a C# project which 
is a class library and contains only one class with these two functions. Also note 
that i have deleted the InteropDLLs dir inside the VB Project Folder.  So u need to 
copy this folder from my previous solution post  and paste it inside the VB Project 
folder before running the app, as im referring to the DLL path  in form1.vb.     
regards,   sr  
View Attachment(s):
http://groups.msn.com/BDotNet/_notifications.msnw?type=msg&parent=1&item=3186
-----------------------------------------------------------

To stop getting this e-mail, or change how often it arrives, go to your E-mail 
Settings.
http://groups.msn.com/BDotNet/_emailsettings.msnw

Need help? If you've forgotten your password, please go to Passport Member Services.
http://groups.msn.com/_passportredir.msnw?ppmprop=help

For other questions or feedback, go to our Contact Us page.
http://groups.msn.com/contact

If you do not want to receive future e-mail from this MSN group, or if you received 
this message by mistake, please click the "Remove" link below. On the pre-addressed 
e-mail message that opens, simply click "Send". Your e-mail address will be deleted 
from this group's mailing list.
mailto:[EMAIL PROTECTED]

Reply via email to