Random Musings

June 25, 2009

Type.GetType() return null value

Filed under: Dynamics CRM — haditeo @ 9:06 pm

Here are my code snippet that returning null value when invoking Type.GetType()

        public Opportunity ToOpportunity(DynamicEntity de)
        {
            try
            {
                Assembly sdkDll = Assembly.LoadFrom("microsoft.crm.sdk.dll");

                Opportunity newOpportunity = new Opportunity();

                foreach (PropertyInfo pi in typeof(Opportunity).GetProperties())
                {
                    string crmFieldMapping = ((CrmFieldMapping)(pi.GetCustomAttributes(typeof(CrmFieldMapping), false))[0]).GetCrmFieldMapping();
                    string crmTypeProperty = ((CrmTypeProperty)(pi.GetCustomAttributes(typeof(CrmTypeProperty), false))[0]).GetCrmTypeProperty();

                    if (de.Properties.Contains(crmFieldMapping))
                    {
                        Type currentCrmTypeProperty = sdkDll.GetType("Microsoft.Crm.Sdk." + crmTypeProperty);

                        object obj = Activator.CreateInstance(currentCrmTypeProperty, crmFieldMapping, de[crmFieldMapping]);

                        pi.SetValue(newOpportunity, obj, null);
                    }
                }

                return newOpportunity;
            }
            catch (SoapException soapExc)
            {
                throw soapExc;
            }
            catch (Exception exc)
            {
                throw exc;
            }

        }

The above codes are working well when i have tested by testing the dll using NUnit. But when i deployed it as a custom workflow activity, these code snippet returned null value. There is an IOException complained by the system. In the details of the Exception, it’s mentioned that it cannot find the microsoft.crm.sdk.dll at the ‘c:\windows\system32’. I have tried to copy the code manually to \windows\system32 but it still cannot find the correct dll, maybe i should register it through the GAC.

Type currentCrmTypeProperty = sdkDll.GetType("Microsoft.Crm.Sdk." + crmTypeProperty);

Anyway, i have another alternative by directly using Type.GetType as shown below. I was reluctant to use the Full Assembly Qualified Name, but somehow when referring to microsoft.crm.sdk.KeyProperty, it still cannot find, unless i specified Full Qualified Name

 Type currentCrmTypeProperty = Type.GetType("Microsoft.Crm.Sdk." + crmTypeProperty + ", Microsoft.Crm.Sdk, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"); 

And here are the modified code

        public Opportunity ToOpportunity(DynamicEntity de)
        {
            try
            {
                Opportunity newOpportunity = new Opportunity();

                foreach (PropertyInfo pi in typeof(Opportunity).GetProperties())
                {
                    string crmFieldMapping = ((CrmFieldMapping)(pi.GetCustomAttributes(typeof(CrmFieldMapping), false))[0]).GetCrmFieldMapping();
                    string crmTypeProperty = ((CrmTypeProperty)(pi.GetCustomAttributes(typeof(CrmTypeProperty), false))[0]).GetCrmTypeProperty();

                    if (de.Properties.Contains(crmFieldMapping))
                    {
                        Type currentCrmTypeProperty = Type.GetType("Microsoft.Crm.Sdk." + crmTypeProperty + ", Microsoft.Crm.Sdk, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"); 
                                                 
                        object obj = Activator.CreateInstance(currentCrmTypeProperty, crmFieldMapping, de[crmFieldMapping]);

                        pi.SetValue(newOpportunity, obj, null);
                    }
                }

                return newOpportunity;
            }
            catch (SoapException soapExc)
            {
                throw soapExc;
            }
            catch (Exception exc)
            {
                throw exc;
            }

        }

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.