Changetype无法更改为create<Convert.ChangeType(type)>(type);
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
//using System.Collections.Generic;
using System.Reflection;
namespace FixUtil
{
class Program
{
static void Main(string[] args)
{
FIXMessage fix = new FIXMessage();
fix.top1 = "FIXML";
fix.top2 = "FIXMLMessage";
XMLUtil go = new XMLUtil(fix);
XDocument xdoc = new XDocument(new XDeclaration("1.0", "UTF-8", "yes"), new XElement("FIXML"));
XElement top2 = new XElement("FIXMLMessage", null);
XElement Header = go.createHeader();
top2.Add(Header);
XElement ApplicationMessage = go.createApplicationMessage();
top2.Add(ApplicationMessage);
XElement order_element = go.createOrder();
ApplicationMessage.Add(order_element);
//Order.Add(go.createClOrderID("1968"));
Order order_instance = new Order();
order_instance.CIOrdID = "1968";
order_instance.instrument = new Instrument();
order_instance.instrument.Symbol = "1171";
create<Order>(order_element, order_instance);
//create<Order>(order_element);
xdoc.Root.Add(top2);
xdoc.Save("./FIX.xml");
while (true)
{ }
}
//protected static void create<T>(XElement root) where T : new()
protected static void create<T>(XElement root, T instance) where T : class
{
//MethodInfo[] methodInfos = typeof(T).GetMethods(BindingFlags.Public | BindingFlags.Static);
if (typeof(T).GetProperties().Count() == 0)
return;
foreach (PropertyInfo memberinfo in typeof(T).GetProperties())
{
string type = memberinfo.PropertyType.ToString().Split('.')[1].ToString();
XElement child;
if (type == "String")
{
root.Add(new XElement(memberinfo.Name, memberinfo.GetValue(instance, null)));
//root.Add(new XElement(memberinfo.Name, null));
}
else
{
child = new XElement(memberinfo.Name, null);
root.Add(child);
create(child, memberinfo.GetType());
//create(child, instance);
//create<Type.GetType(type)>(child);
}
Console.WriteLine(memberinfo.Name + " "
+ memberinfo.PropertyType.ToString().Split('.')[1].ToString() + " "
//+ memberinfo.GetValue(instance, null).ToString() + " "
+ memberinfo.PropertyType);
}
return;
}
protected static void create(XElement root, dynamic instance)
{
//return null;
//root.Add(new XElement(instance, GetValue(instance, null)));
//child = new XElement(root, null);
//create(child);
//MethodInfo[] methodInfos = instance.GetMethods(BindingFlags.Public | BindingFlags.Static);
if(instance.GetProperties().Count() == 0)
return;
foreach (PropertyInfo memberinfo in instance.GetProperties())
{
string type = memberinfo.PropertyType.ToString().Split('.')[1].ToString();
XElement child;
if (type == "String")
{
//root.Add(new XElement(memberinfo.Name, memberinfo.GetValue(instance, null)));
root.Add(new XElement(memberinfo.Name, null));
}
else
{
child = new XElement(memberinfo.Name, null);
root.Add(child);
create(child, memberinfo.GetType());
//create(child, instance);
//create<Type.GetType(type)>(child);
}
Console.WriteLine(memberinfo.Name + " "
+ memberinfo.PropertyType.ToString().Split('.')[1].ToString() + " "
//+ memberinfo.GetValue(instance, null).ToString() + " "
+ memberinfo.PropertyType);
}
return;
}
}
}
答案 0 :(得分:1)
Convert.ChangeType(type)
不是类型参数。这是函数调用。您必须在尖括号内提供文字类型或泛型类型参数。您无法使用System.Type
引用,也无法使用object
引用。
答案 1 :(得分:0)
要调用此方法:
create<Person>(xmlDoc);
或
dynamic obj = create(xmlDoc);
并提供不带任何类型的create函数的重载。哪个更干净,因为现在你不必知道将返回什么类型。
答案 2 :(得分:0)
如果您需要的类型不是T,那么您尝试使用泛型是不可能的。
您要求的值必须是通用值或编译时已知的值。
这意味着您唯一的选择是:
create<T>(root);
或者:
create<PureTypeName>(root);
或添加其他通用参数。