How to convert objects in reflection

2019-09-08 08:01发布

问题:

I have this piece of code which is working fine, My question is if i want to add another element to the xml named 'Allow'. the Allow Element can get the values of 'True' or 'False'. I want to convert it to a boolean property in my Plugin class, meaning the Plugin will have the additional property:

public bool Allow { get; set; }

is there a possibility to convert it?

can you give a code example?

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
            //
        }
    }
}

回答1:

change

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

to

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);
}

PS: use <Allow>true</Allow> not <Allow>True</Allow>



标签: c# reflection