none
Need to convert string into class name RRS feed

  • Question

  • Hi,

    I will explain the issue,

    I have developed driver script and it reads the values like class name and test method name in .csv file. When its reading the values its taking as string.

    Task: I need to execute the methods from the csv file using the class names.

    I have tried using Type, but didn't find any solution.

    Our suggestions needed.

    Friday, December 12, 2014 1:07 PM

Answers

All replies

  • Refer following link

    http://stackoverflow.com/questions/223952/create-an-instance-of-a-class-from-a-string


    Gaurav Khanna | Microsoft VB.NET MVP | Microsoft Community Contributor

    Friday, December 12, 2014 4:34 PM
  • This is what you want-

    using System;
    using System.Runtime.Remoting;
    
    public class Example
    {
       public static void Main()
       {
          // First param - Namespace name of the class
          // Second param - Actual class name
          Object obj= Activator.CreateInstance("PersonInfo", "Person");
    
         // Cast obj to your actual object
         Person person = (Person)obj;
       }
    }

    Saturday, December 13, 2014 2:41 PM
  • Hi All,

    I have resolved the above issue.

    I have passed classname and testmethod names as paramters to the function.

    I have placed the below code in function.

    Type type = Type.GetType(classname);
    object obj = Activator.CreateInstance(type);
    MethodInfo methodInfo = type.GetMethod(mymethod);
    methodInfo.Invoke(obj, null); 

    Wednesday, September 30, 2015 12:52 PM