类型对象的运行时强制转换

时间:2022-12-08 11:38:29

I'd like to instantiate an object of a generic class during run-time; I call a method which gives me back a Type Object; I'd like to convert this generic class into a specific class, and then instantiate objects of this class. Is it possible? I used to write in Java:

我想在运行时实例化一个泛型类的对象;我调用一个方法,它给了我一个Type对象;我想将这个泛型类转换为一个特定的类,然后实例化该类的对象。可能吗?我以前用Java写的:

Class<DBConnectionProvider> dBConnectionProviderClass =
              (Class<DBConnectionProvider>)Configuration.getInstance().getDbConnectionProviderClass();

The method getDbConnectionProviderClass() returns a Class Object which is converted on run-time; In my C# application this method returns a Type object; is it possible to convert this in DBConnectionProvider and instantiate a class of this? Thank you for your answers.

getDbConnectionProviderClass()方法返回一个在运行时转换的Class对象;在我的C#应用​​程序中,此方法返回一个Type对象;是否可以在DBConnectionProvider中转换它并实例化一个类?谢谢您的回答。

2 个解决方案

#1


0  

Once you have the type object you just need to call:

一旦你有了类型对象,你只需要调用:

object o = Activator.CreateInstance([your type]).Unwrap();

or if you need to supply constructor arguments:

或者如果您需要提供构造函数参数:

object o = Activator.CreateInstance([your type], obj1,obj2...).Unwrap();

And then cast to your type.

然后投射到你的类型。

#2


0  

Simple example of creating instances of classes with reflection (Java)

使用反射创建类实例的简单示例(Java)

import java.awt.Rectangle;

public class SampleNoArg {

   public static void main(String[] args) {
      Rectangle r = (Rectangle) createObject("java.awt.Rectangle");
      System.out.println(r.toString());
   }

   static Object createObject(String className) {
      Object object = null;
      try {
          Class classDefinition = Class.forName(className);
          object = classDefinition.newInstance();
      } catch (InstantiationException e) {
          System.out.println(e);
      } catch (IllegalAccessException e) {
          System.out.println(e);
      } catch (ClassNotFoundException e) {
          System.out.println(e);
      }
      return object;
   }
}

#1


0  

Once you have the type object you just need to call:

一旦你有了类型对象,你只需要调用:

object o = Activator.CreateInstance([your type]).Unwrap();

or if you need to supply constructor arguments:

或者如果您需要提供构造函数参数:

object o = Activator.CreateInstance([your type], obj1,obj2...).Unwrap();

And then cast to your type.

然后投射到你的类型。

#2


0  

Simple example of creating instances of classes with reflection (Java)

使用反射创建类实例的简单示例(Java)

import java.awt.Rectangle;

public class SampleNoArg {

   public static void main(String[] args) {
      Rectangle r = (Rectangle) createObject("java.awt.Rectangle");
      System.out.println(r.toString());
   }

   static Object createObject(String className) {
      Object object = null;
      try {
          Class classDefinition = Class.forName(className);
          object = classDefinition.newInstance();
      } catch (InstantiationException e) {
          System.out.println(e);
      } catch (IllegalAccessException e) {
          System.out.println(e);
      } catch (ClassNotFoundException e) {
          System.out.println(e);
      }
      return object;
   }
}