2011-08-31 157 views
3

我有如下功能。C#,将字符串变量转换为类变量

public static object getClassInstance(string key, object constructorParameter) 
{ 
     // body here 
} 

关键变量将有我的班级名称。我需要返回该类的新实例。如果constructorParm为null,那么我需要使用默认构造函数加载类,否则传递构造函数参数。我该怎么做呢 ?

地址:

我写的代码一样传递给函数是 “CReportBO” 这

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Reflection; 
using Catalyst.BO.StudentProfileBO; 
using Catalyst.BO.ReportBO; 
using Catalyst.DAL.ReportDAO; 
using System.Collections; 
using System.Data; 

namespace Catalyst.BO.Factory 
{ 
    public class CFactory 
    { 
     public static object getClassInstance(string key, params object[] constructorArgs) 
     { 
      string assemblyPath = null; 
      string customClassName = key.Substring(0, 1) + "Custom" + key.Substring(1); 

      DataSet objDataset = getAssemblyInfo(key); 
      if (objDataset != null && objDataset.Tables.Count > 0 && objDataset.Tables[0].Rows.Count > 0) 
      { 
       assemblyPath = objDataset.Tables[0].Rows[0]["ACA_ASSEMBLY_PATH"].ToString(); 
      } 

      Assembly assembly; 
      Type type; 

      if (assemblyPath != null && assemblyPath != string.Empty) 
      { 
       assembly = Assembly.LoadFile(assemblyPath); 
       type = assembly.GetType(customClassName); 
      } 
      else // if no customisation 
      { 
       type = Type.GetType(key); 
      } 

      object classInstance = constructorArgs == null ? Activator.CreateInstance(type) : Activator.CreateInstance(type, constructorArgs); 
      if (classInstance == null) throw new Exception("broke"); 
      return classInstance; 
     } 
    } 
} 

关键。 CReportBO可以在函数的范围内访问。但在/ /如果没有自定义部分(即type = Type.GetType(key)),类型返回我空。怎么了 ?

+1

如果您想使用'null' *作为参数*; p –

+0

i don hai ve dat条件的nw。但我会处理它。我如何处理为什么现有的情况? –

+0

@Nithesh:请注意,您可以通过缩进四个空格来[将行格式化为代码](http://meta.stackexchange.com/questions/22186/how-do-i-format-my-code-blocks)。编辑器工具栏中的“{}”按钮可以为您做到这一点。其他人在这个问题上为你做了,但下次你应该自己尝试。单击编辑器工具栏中的橙色问号以获取更多信息和格式化提示。 – outis

回答

7

如果key要么是集限定名称空间限定无论是核心组件或调用的组件,然后内

Type type = Type.GetType(key); 
return constructorParameter == null ? Activator.CreateInstance(type) 
      : Activator.CreateInstance(type, constructorParameter); 

我不知道,不过,如果:

public static object getClassInstance(string key, params object[] args) 

更灵活,允许:

Type type = Type.GetType(key); 
return Activator.CreateInstance(type, args); 

与用法如:

object o = getClassInstance(key); // uses default constructor 
object o = getClassInstance(key, null); // passes null to single parameter 
object o = getClassInstance(key, 123, "abc"); // etc 
+1

heh。是的 - 已投票删除我的帖子。 – fatty

+0

您添加了可以将consturctorParameter更改为支持额外构造函数的参数数组的信息,我认为值得保留。 –

+0

是的,dat值得保留,plz添加它。 –

1

你应该能够做这样的事情:

Activator.CreateInstance(Type.GetType(keyBindings), constructorParameters)

你可以改变你的方法的签名是public static object getClassInstance(string key, params object[] constructorParameters),如果你想允许的可能性多个构造函数参数。

0

你将要与此类似:

public object CreateMyObject(string name, object constructorParam) 
    { 
     Type t = Type.GetType(name); 
     if (t != null) 
     { 
      object o = Activator.CreateInstance(t, constructorParam != null ? new[] { constructorParam } : null); 
      return o; 
     } 

     return null; 
    } 

当然,你将需要添加相应的错误处理。

妈的,由Marc打吧,他的回答将优先于矿山:)

0

首先,你应该从你的字符串类型:

Type t = testAssembly.GetType(key); 

然后创建您的类型的实例:

Object o = Activator.CreateInstance(t, constructorParameter); 

而且o是一个对象的键入