13

我所拥有的是一个类的集合,它们都实现了相同的接口,但在底层可能会有很大的不同。我想要一个配置文件控制在启动程序时哪些类进入集合,看起来像:

<class1 prop1="foo" prop2="bar"/>

并将其变成:

blah = new class1();
blah.prop1="foo";
blah.prop2="bar";

以一种非常通用的方式。我不知道该怎么做是获取prop1配置文件中的字符串并将其转换为代码中的实际属性访问器。C# 中是否有任何元编程工具允许这样做?

4

8 回答 8

9

将类序列化到/从 xml 可能更容易,然后您可以简单地将 XmlReader(正在读取您的配置文件)传递给反序列化器,它会为您完成其余的工作。

这是一篇很好的关于序列化的文章

编辑

我想补充一件事,即使反射很强大,它也需要你了解一些关于类型的东西,比如参数等。

序列化为 XML 不需要任何这些,您仍然可以通过确保将完全限定的类型名称写入 XML 文件来获得类型安全,因此会自动加载相同的类型。

于 2008-08-27T20:47:43.293 回答
9

反射允许你这样做。您可能还想查看XML 序列化

Type type = blah.GetType();
PropertyInfo prop = type.GetProperty("prop1");
prop.SetValue(blah, "foo", null);
于 2008-08-27T20:51:13.270 回答
8

我还建议像其他人已经提到的那样进行 Xml 序列化。这是我放在一起演示的示例。属性用于将 Xml 中的名称连接到数据结构中的实际属性名称和类型。属性还列出了可以进入Things集合的所有允许类型。此集合中的所有内容都必须具有公共基类。您说您已经有一个通用接口——但您可能必须将其更改为抽象基类,因为此代码示例在Thing接口时无法立即工作。

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main()
        {
            string xml =
                "<?xml version=\"1.0\"?>" + 
                "<config>" +
                "<stuff>" + 
                "  <class1 prop1=\"foo\" prop2=\"bar\"></class1>" +
                "  <class2 prop1=\"FOO\" prop2=\"BAR\" prop3=\"42\"></class2>" +
                "</stuff>" +
                "</config>";
            StringReader sr = new StringReader(xml);
            XmlSerializer xs = new XmlSerializer(typeof(ThingCollection));
            ThingCollection tc = (ThingCollection)xs.Deserialize(sr);

            foreach (Thing t in tc.Things)
            {
                Console.WriteLine(t.ToString());
            }
        }
    }

    public abstract class Thing
    {
    }

    [XmlType(TypeName="class1")]
    public class SomeThing : Thing
    {
        private string pn1;
        private string pn2;

        public SomeThing()
        {
        }

        [XmlAttribute("prop1")]
        public string PropertyNumber1
        {
            get { return pn1; }
            set { pn1 = value; }
        }

        [XmlAttribute("prop2")]
        public string AnotherProperty
        {
            get { return pn2; }
            set { pn2 = value; }
        }
    }

    [XmlType(TypeName="class2")]
    public class SomeThingElse : SomeThing
    {
        private int answer;

        public SomeThingElse()
        {
        }

        [XmlAttribute("prop3")]
        public int TheAnswer
        {
            get { return answer; }
            set { answer =value; }
        }
    }

    [XmlType(TypeName = "config")]
    public class ThingCollection
    {
        private List<Thing> things;

        public ThingCollection()
        {
            Things = new List<Thing>();
        }

        [XmlArray("stuff")]
        [XmlArrayItem(typeof(SomeThing))]
        [XmlArrayItem(typeof(SomeThingElse))]
        public List<Thing> Things
        {
            get { return things; }
            set { things = value; }
        }
    }
}
于 2008-08-28T01:43:22.007 回答
6

您正在寻找反射或 XML 序列化。

使用反射,您可以使用类似这样的方式查找类型

public IYourInterface GetClass(string className)
{
    foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) 
    {            
        foreach (Type type in asm.GetTypes())
        {
            if (type.Name == className)
                return Activator.CreateInstance(type) as IYourInterface;
        }   
    }

    return null;
}

请注意,这将遍历所有程序集。您可能希望将其减少为仅包含当前正在执行的程序集。

对于分配属性值,您还可以使用反射。类似的东西

IYourInterface o = GetClass("class1");
o.GetType().GetProperty("prop1").SetValue(o, "foo", null);

虽然反射可能是最灵活的解决方案,但您还应该看看XML-serialization,以便自己跳过繁重的工作。

于 2008-08-27T20:50:51.647 回答
3

大量的元编程设施。

具体来说,您可以获取对包含这些类的程序集的引用,然后轻松地Type从其名称中获取类的。请参阅Assembly.GetType 方法(字符串)

Activator从那里,您可以使用或自身的构造函数来实例化类Type。请参阅Activator.CreateInstance 方法

拥有实例后,您可以再次使用该Type对象来设置属性。请参阅Type.GetProperty 方法和/或Type.GetField 方法以及PropertyInfo.SetValue 方法

于 2008-08-27T20:48:32.900 回答
1

我最近做了一些非常相似的事情,我使用了一个抽象工厂。其实这里可以看到基本概念:

抽象工厂设计模式

于 2008-08-28T01:53:51.543 回答
1

我认为您可以在这里使用 Dynamics。创建 ExpandoObject,它可以用作字典,用于从 xml 配置中设置属性。

于 2012-09-21T11:33:26.263 回答
0

反射是你想要的。反射 + 类型转换器。没有太多时间来解释了,只要用谷歌搜索一下,你应该一切顺利。或者您可以只使用 xml 序列化程序,但是您必须遵守格式,但效果很好。

于 2008-08-27T20:47:19.363 回答