Changetype can not change to type 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;
}
}
}
What you are trying to do with generics is impossible if the type you need is not T.
The value that goes where you are asking for needs to be a generic value or a value known at compile time.
That means your only choice is:
Or:
Or adding other generic parameters.