简体   繁体   中英

Type.GetType(“{namespace.classname}{assemblyname}”) not working in c#

  1. Type type1=Type.GetType({namespace.classname}{assemblyname}) //string value
  2. Type type =typeof(namespace.class name);//class name

If I put both of this together then the type1 loads the value but when only line 1 is run then type1 value is null, even they both are two separate variables It's a strange issue

I need to use activator and invoke method class and method with a string

I don't know if I understand your question. It seems like you're looking for information on how to use System.Activator . It always helps when you're asking a question if you can provide as much code, context, and clarity as possible to help guide people in giving an answer to your question. Please consider spending some time looking through the instructions and examples in Microsoft's documentation .

I wrote a couple of lines of sample code to illustrate that Activator can accept parameters following the Type argument. The code illustrates that the Person object can be created through the constructor that requires a string parameter by adding that argument after the typeof(Person) argument in Activator.CreateInstance . Hopefully this helps somewhat.

class Person
{
    public string Name { get; set; }

    public Person(string name)
    {
        Name = name;
    }
}
    
class Program
{
    static void Main(string[] args)
    {
        var result = Activator.CreateInstance(typeof(Person), "Joe");
        if (result is Person person)
        {
            Console.WriteLine("Created new person:");
            Console.WriteLine(person.Name);
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM