Getting Class Type from String

Getting Class type from String

Class<?> cls = Class.forName(className);

But your className should be fully-qualified - i.e. com.mycompany.MyClass

Get a class type using a string

Unfortunately, you can't just do that but you can use reflection to do this. An example would look like:

using System;
using System.Collections.Generic;

namespace StackOverflow
{
public class MyClass
{
}

public class Program
{
static void Main(string[] args)
{
string className = "StackOverflow.MyClass";

// Generating the generic list
var type = Type.GetType(className);
var listType = typeof(List<>).MakeGenericType(new[] { type });
var list = Activator.CreateInstance(listType);

// Adding a value to the list
var value = new MyClass();
list.GetType().GetMethod("Add").Invoke(list, new object[] { value });

// Fetch a value of the list
var myClass = (MyClass)list.GetType().GetMethod("get_Item").Invoke(list, new object[] { 0 });

// Invoke methods of MyClass-instance
Console.WriteLine(myClass.ToString());

// Keep console open
Console.ReadLine();
}
}
}

As madreflection has mentioned in the comments above, you can find additonal examples here.

To clear things up a bit, you could use a list of object instead of creating the list via reflection.

string className = "StackOverflow.MyClass";
var type = Type.GetType(className);

// Generating the list of objects
var list = new List<object>();

// Adding a value to the list
var value = Activator.CreateInstance(type);
list.Add(value);

// Fetch a value from the list
var myClass = (MyClass)list[0];

// Invoke methods of MyClass-instance
Console.WriteLine(myClass.ToString());

How to convert String type to Class type in java

Class<?> classType = Class.forName(className);

Make sure className is fully qualified class name like com.package.class Also, please share your error message that you see.

Get the name of a Dart class as a Type or String

The class type can be used as a Type:

Type myType = MyClass;

How to convert a string with the name of a class to the class type itself?

You can get your class back from string, but you need to use your project's module name while getting class name. If you don't use your module name then it will return nil because the class name you have referenced earlier is not fully qualified by the module name. You should change the class name string to represent the fully qualified name of your class:

let myClassString = String(MyModule.MyViewController.self)
print(myClassString)
let myClass = NSClassFromString("MyModule.\(myClassString)") as! MyViewController.Type
print(myClass)

how to get class name from String

You made a mistake on this line :

reader.setTargetType((Class<? extends className>) className.class);

It should be :

reader.setTargetType(cls);

Getting class by its name

use forName instead..

something like this..

 try {
Class<?> act = Class.forName("com.bla.TestActivity");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}

Get class by string value

Yes, use Reflection.

You can use Type.GetType to get an instance of the Type for the class by string, then instantiate it using Activator.CreateInstance, something like this:

public MyObject Create(string pattern)
{
Type t = Type.GetType(pattern);
if (t == null) {
throw new Exception("Type " + pattern + " not found.");
}
return Activator.CreateInstance(t);
}

You could use Activator.CreateInstance(string, string) overload also, but this would not directly return a new instance of the Type required.



Related Topics



Leave a reply



Submit