C#,将字符串变量转换为类变量

时间:2011-08-31 07:02:10

标签: c# class reflection constructor

我的功能如下。

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

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

ADD:

我写了这样的代码

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”。 CReportBO可在函数范围内访问。但是在//如果没有自定义部分(即type = Type.GetType(key)),则返回null。怎么了?

5 个答案:

答案 0 :(得分: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 :(得分:1)

你应该可以这样做:

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

如果要允许多个构造函数参数,可以将方法签名更改为public static object getClassInstance(string key, params object[] constructorParameters)

答案 2 :(得分: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殴打,他的回答优先于我的:)

答案 3 :(得分:0)

您可以使用C#Activator.CreateInstance方法实现类似的功能。

查看此MSDN文档http://msdn.microsoft.com/en-us/library/dd384354.aspx

使用Activator类可以做更多事情。

干杯,B

答案 4 :(得分:0)

首先,您应该从字符串中获取类型:

Type t = testAssembly.GetType(key);

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

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

o是您类型的对象