Another Reflection Question

Hi,
I am working on using Reflection for a plug-in application, using dlls, the app will be done in C+.NET.
I have been able to see the meta-data in the dlls, and that is about it.
How can I take a class that is in the dll and create an imstance of it Is there a method within the Reflection class, MethodInfo, that I can use to do that

Thanks in Advance
Hoop


Answer this question

Another Reflection Question

  • Florian_TT

    Hi,

    The classInstance.GetType() is just for you to see the type of the instance. Activator.CreateInstance is the actual method that creates the class instance. In the sample code, the class instance is stored as Object so you won't be able to invoke methods specific to a type (or class). You can cast classInstance to specific type and invoke its methods like:


    DirectCast(classInstance, MyClass).MyMethod()   ' This will invoke MyMethod method in MyClass class.
     


    On the other hand, if you know the method's name that you want to execute at runtime, you can also to something like this:


    assemblyType.InvokeMember("MyMethod", BindingFlags.InvokeMethod, Nothing, classInstance, New Object() {})   ' This will also invoke MyMethod method in MyClass class. Please refer to my previous post for reference.

     


  • tomk_vsts_user

    Hi,
    So when this function is run will the NotificationDataObject come into existance and start doing what it does
    This is where I believe I am having a problem. I get to this point where I have the data but does not start the code running.

    Thanks
    Hoop

  • Daniel Lord

    Hi,
    Below is what I am doing. Not sure how to get an instance of the class from the Activator::CreateInstance(prtrType).
    Should that line actually start the class

    Hoop



    void SearchPlugInDirectory( String *currentDirectory )< xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />

          {

                                                                int indexStr = 0;

                                                                //For the dll Assemblies                                                                                     

                                                                Assembly *plugInAssemblies;

                                                                Type *ptrType;

                                                                Object *aModule;

                                                                String *moduleName;

                                                                MemberInfo *methodName;

                                                                String *methodType;

                                                                //Type* iFace = InputModule::IOModule;

                                                               

                                                                                                                           

             try

                                                     {

                String *fileName = S"";

     

                                                                                        // get list of files in current directory

                String *fileArray[] =  Directory::GetFiles( currentDirectory );

     

                // iterate through list of files

                for ( int dllFile = 0; dllFile < fileArray->Length; dllFile++ )

                                                                            {

                                                                                                    //Load dlls and types

                                                                                                    fileName = fileArray[ dllFile ];

                                                                                          plugInAssemblies = Assembly::LoadFrom(fileName);

                                                               

     

                                                                                                    Type* types __gc[] = plugInAssemblies->GetTypes();

                                                                                       

                                                                                         

                                                                                         MethodInfo* buildModuleMethod = plugInAssemblies->GetTypes() [0]->GetMethod(S"BaseModuleClass");

                                                                           

                                                                                                    ptrType = types[dllFile];

                                                                                                    moduleName = types[dllFile]->FullName; //the name

                                                                                                   

                                                                                                    aModule = Activator::CreateInstance(ptrType);

                                                                                                    aModule->GetType();

                                                                                                               

                                                                                                   

                                                                            } // end for

                                        

     

          } // end method SearchPlugInDirectory


  • AkChauhan

    Hi,
    I guess this is where I am pretty much stumped. What to do with aModule. I cannot run anything with it, such as aModule->Build(); since the base app does not know that Build exists yet.
    Hoop

  • Bar?? Soner U?akl?

    In your case, aModule is an instance of whatever ptrType happens to be and the instance is created by Activator::CreateInstance(ptrType).

  • Infiniti

    You can use the Activator class to create class instances at runtime like this:

            Dim myAssembly As Assembly = Reflection.Assembly.LoadFrom("Assembly.dll")

            Try
                For Each assemblyType As Type In myAssembly.GetTypes()
                    If assemblyType.IsClass() AndAlso Not assemblyType.IsSealed() Then
                        Dim classInstance As Object = Activator.CreateInstance(assemblyType)
                        Debug.WriteLine(classInstance.GetType())
                    End If
                Next
            Catch ex As Exception
                Debug.WriteLine(ex.ToString())
            End Try

    Good Luck!



  • mdegi

    Hi,
    I do have it that far in C++. I am stummped at the classInstance.GetType(), mine is actually, aModule->GetType();.
    Does not really do anything when it runs that line.
    Is that what is suppose to create the class instance

    Thanks
    Hoop

  • GreyWolf

    An instance of a NotificationDataObject is created. The NotificationDataObject class is an ancestor class and this method creates an instance of one of its children. After it is run, the object is created and ready to be used. Methods can be called on it, properties can be accessed. There is no "start the code" point, as it is just instantiating an object.
  • Michael Baarz

    To give you a C# example, with a different method of instantiation, here is what I've done in the past.



    private NotificationDataObject GetDataObject(string objectName)

    {

    Object[] args = new Object[] { };

    Type t = Type.GetType("Notification.Data." + objectName + ", Notification.Data",

    true, true);

    object result = t.InvokeMember(objectName, System.Reflection.BindingFlags.CreateInstance, null, null, args);

    return (NotificationDataObject) result;

    }

     

  • Andru

    Hi,
    I finnally got it. Took a cast,
    BaseModuleClass *plugin = dynamic_cast<BaseModuleClass *>(instance);
    pulgin->buildModule();

    Not sure if that is the best way but the dll did start running.

    Thanks For the direction
    Hoop

  • Another Reflection Question