C# 如何转换反射中的对象

C# 如何转换反射中的对象,c#,reflection,C#,Reflection,我有一段代码运行良好, 我的问题是,是否要向xml中添加另一个名为“Allow”的元素。Allow元素可以获取'True'或'False'的值。我想在我的插件类中将其转换为布尔属性,这意味着插件将具有附加属性: public bool Allow { get; set; } 是否有可能将其转换 你能给出一个代码示例吗 using System; using System.Collections.Generic; using System.Linq; using System.IO; using

我有一段代码运行良好, 我的问题是,是否要向xml中添加另一个名为“Allow”的元素。Allow元素可以获取'True'或'False'的值。我想在我的插件类中将其转换为布尔属性,这意味着插件将具有附加属性:

public bool Allow { get; set; }
是否有可能将其转换

你能给出一个代码示例吗

using System;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using System.Xml.Linq;
using System.Reflection;
using System.Text;

namespace WindowsFormsApplication1
{
    public abstract class Plugin
    {
        public string Type { get; set; }
        public string Message { get; set; }
    }

    public class FilePlugin : Plugin
    {
        public string Path { get; set; }
    }

    public class RegsitryPlugin : Plugin
    {
        public string Key { get; set; }
        public string Name { get; set; }
        public string Value { get; set; }
    }

    static class MyProgram
    {
        [STAThread]
        static void Main(string[] args)
        {
            string xmlstr =@"
                <Client>
                  <Plugin Type=""FilePlugin"">
                    <Message>i am a file plugin</Message>
                    <Path>c:\</Path>
                  </Plugin>
                  <Plugin Type=""RegsitryPlugin"">
                    <Message>i am a registry plugin</Message>
                    <Key>HKLM\Software\Microsoft</Key>
                    <Name>Version</Name>
                    <Value>3.5</Value>
                  </Plugin>
                </Client>
              ";

            Assembly asm = Assembly.GetExecutingAssembly();
            XDocument xDoc = XDocument.Load(new StringReader(xmlstr));
            Plugin[]  plugins = xDoc.Descendants("Plugin")
                .Select(plugin =>
                {
                    string typeName = plugin.Attribute("Type").Value;
                    var type = asm.GetTypes().Where(t => t.Name == typeName).First();
                    Plugin p = Activator.CreateInstance(type) as Plugin;
                    p.Type = typeName;
                    foreach (var prop in plugin.Descendants())
                    {
                        type.GetProperty(prop.Name.LocalName).SetValue(p, prop.Value, null);
                    }

                    return p;
                }).ToArray();

            //
            //"plugins" ready to use
            //
        }
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.IO;
使用System.Xml.Linq;
运用系统反思;
使用系统文本;
命名空间Windows窗体应用程序1
{
公共抽象类插件
{
公共字符串类型{get;set;}
公共字符串消息{get;set;}
}
公共类文件插件:插件
{
公共字符串路径{get;set;}
}
公共类RegsitryPlugin:Plugin
{
公共字符串密钥{get;set;}
公共字符串名称{get;set;}
公共字符串值{get;set;}
}
静态类MyProgram
{
[状态线程]
静态void Main(字符串[]参数)
{
字符串xmlstr=@”
我是一个文件插件
c:\
我是一个注册表插件
HKLM\Software\Microsoft
版本
3.5
";
Assembly asm=Assembly.getExecutionGassembly();
XDocument xDoc=XDocument.Load(新的StringReader(xmlstr));
Plugin[]plugins=xDoc.substands(“插件”)
.选择(插件=>
{
字符串typeName=plugin.Attribute(“Type”).Value;
var type=asm.GetTypes()。其中(t=>t.Name==typeName.First();
Plugin p=Activator.CreateInstance(类型)作为插件;
p、 类型=类型名;
foreach(plugin.subjects()中的var prop)
{
type.GetProperty(prop.Name.LocalName).SetValue(p,prop.Value,null);
}
返回p;
}).ToArray();
//
//“插件”随时可用
//
}
}
}
更改

foreach (var prop in plugin.Descendants())
{
    type.GetProperty(prop.Name.LocalName).SetValue(p, prop.Value, null);
}

PS:
使用
true
true
谢谢,你能详细说明为什么使用true和not true吗?事实上,我认为这句话是
bool b=true
写的。我不是100%肯定。也许真的也可以。试试看。
foreach (var prop in plugin.Descendants())
{
    var pi = type.GetProperty(prop.Name.LocalName);
    object newVal = Convert.ChangeType(prop.Value, pi.PropertyType);
    pi.SetValue(p, newVal, null);
}