none
Convert object or class from the string name RRS feed

  • Question

  • Dear all,

    How to convert class or object from the class string??

    static class Factory
        {
            public static IFileHandler Get(FileTransferSS_POSVO fileTransferVO)
            {
                switch (fileTransferVO.FileType)
                {
                    case FILE_TRANSFER_FILE_TYPE_OCTOPUS:
                        Type type = Type.GetType(CMN_ID.FILE_TRANSFER_FILE_OCOTPUS);
                        return Type.GetConstructor(type);     // return IFileHandler object/class for used below code???               
                }
            }
        }


    // ------------------- One function --------------------------------------
      {
                    var position = Factory.Get(fileTransferVO);
                    position.ProcessSaveFile(fileTransferVO.TargetDir, Data1);
                  
                }

    • Moved by Lisa Zhu Monday, December 10, 2012 7:40 AM CLR related (From:Visual C# General)
    Friday, December 7, 2012 4:07 AM

Answers

  • Assembly UCAsm = Assembly.GetAssembly(typeof(YOURKNOWNASMNAME)); Type[] typesInUCAsm = UCAsm.GetTypes(); if (typesInUCAsm != null && typesInUCAsm.Length >0){ Type typeOfAClass = typesInUCAsm[1]; ConstructorInfo[] constructorsForAClass = typeOfAClass.GetConstructors( BindingFlags.CreateInstance | BindingFlags.Default | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.OptionalParamBinding); if (constructorsForAClass != null) { object objectViaReflection = constructorsForAClass[0].Invoke(null); if (objectViaReflection != null) { instanceAClass = (AClass)objectViaReflection; } }

    }

    Hope you will find your way out.  

    Mohammad Saidul Karim

    Friday, December 7, 2012 7:09 AM

All replies

  • This is not going to be this simple.

    First load Assembly that holds the class or object i.e. Assembly theContainer  = Assembly.Load(...)

    Loop through items in Assembly using FullName or other property you think be unique enough to get your object.

    Once the unique name/attribute match found, Use Reflection details to load you object.

    Hope this will take you to right direction.


    Mohammad Saidul Karim

    Friday, December 7, 2012 5:01 AM
  • As I suppose to be simple. why need to loop through items?

    I only need to return one class.

    Moreover, is there any sample link or sample code?

    Friday, December 7, 2012 5:16 AM
  • Assembly UCAsm = Assembly.GetAssembly(typeof(YOURKNOWNASMNAME)); Type[] typesInUCAsm = UCAsm.GetTypes(); if (typesInUCAsm != null && typesInUCAsm.Length >0){ Type typeOfAClass = typesInUCAsm[1]; ConstructorInfo[] constructorsForAClass = typeOfAClass.GetConstructors( BindingFlags.CreateInstance | BindingFlags.Default | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static | BindingFlags.OptionalParamBinding); if (constructorsForAClass != null) { object objectViaReflection = constructorsForAClass[0].Invoke(null); if (objectViaReflection != null) { instanceAClass = (AClass)objectViaReflection; } }

    }

    Hope you will find your way out.  

    Mohammad Saidul Karim

    Friday, December 7, 2012 7:09 AM
  • Hi Raymond,

    From your description, I ‘d like to move this post to  the most related forum for better support.

    There are more  experts in this aspect, so you may have more luck getting answers.

    Thanks for your understanding.

    Regards,


    Lisa Zhu [MSFT]
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Monday, December 10, 2012 7:39 AM