locked
How to typecast and object from a dynamically loaded assembly? RRS feed

  • Question

  • Hi,

    I'm racking my brain with this and need some direction.

    I want to type cast and object I've extracted from a Dynamically loaded DLL but everything I've tried doesn't work. I just want to pull the stored data out of the type. Below is my code. Any advice appreciated! Thanks.

            private void LoadTestAssm()
            {
                Assembly aMainAssm = Assembly.LoadFrom("C:\\Temp\\MainApp.exe");

                Assembly aUtilitiesAssm = Assembly.LoadFrom("C:\\Temp\\Utilities.dll");

                Type[] aMainAssmTypes = aMainAssm.GetTypes();

                foreach (Type aType in aMainAssmTypes)
                {
                    object[] aCustAttributeColl = aType.GetCustomAttributes(true);
                                   
                    for (int i = 0; i < aCustAttributeColl.Length; i++)               
                    {
                        object x;

                        Type aUtilitiesAssmType = aUtilitiesAssm.GetType("MainApp.Utilities.CustAttribute");

                        if (aCustAttributeColl[i] != null)
                            x = aCustAttributeColl[i]; //I'd like to cast this to aUtilitiesAssmType so I can pull out the stored data but it seems I cannot.
                                                               

                    }//if/foreach
                }//foreach

            }//LoadTestAssm

    --PhB
    Monday, November 17, 2008 3:11 PM

Answers

  • Thanks for the reply.

    Yes, what you said about querying the Reflected object is what I did and I am now able to extract out the data, like this:

                        if (aInterfaceAttributeColl[i] != null)
                        {
                            object y=aInterfaceAttributeColl[i];

                            x = y.GetType().GetProperty("Test").GetValue(y,null);                       
                        }

    --PhB
    • Marked as answer by PhrankBooth Monday, November 17, 2008 3:47 PM
    Monday, November 17, 2008 3:47 PM

All replies

  • It's looking like aUtilitiesAssmType is a class of type Type, which holds information about the type "MainApp.Utilities.CustAttribute".  aUtilitiesAssmType isn't a type itself, but rather an instance of the class Type.  There's a difference.  Nevertheless, you still can't cast x to the desired type, simply because that type resides in a separate assembly, and you're dynamically pulling it out.  You have no assembly reference to the type you're trying to cast to.  In order to use the instance you have, you're going to need to stick with reflection, using reflection to query the properties and fields of the object.  I'm afraid there's no other way around this, short of referencing the library that holds the type you're interested in.
    David Morton - http://blog.davemorton.net/
    Monday, November 17, 2008 3:20 PM
    Moderator
  • Thanks for the reply.

    Yes, what you said about querying the Reflected object is what I did and I am now able to extract out the data, like this:

                        if (aInterfaceAttributeColl[i] != null)
                        {
                            object y=aInterfaceAttributeColl[i];

                            x = y.GetType().GetProperty("Test").GetValue(y,null);                       
                        }

    --PhB
    • Marked as answer by PhrankBooth Monday, November 17, 2008 3:47 PM
    Monday, November 17, 2008 3:47 PM
  • Wouldn't that make my post the answer?
    David Morton - http://blog.davemorton.net/
    Monday, November 17, 2008 3:51 PM
    Moderator