Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将字符串转换为类型?_C# - Fatal编程技术网

C# 如何将字符串转换为类型?

C# 如何将字符串转换为类型?,c#,C#,Changetype不能更改为类型创建(类型) 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用系统文本; 使用System.Xml.Linq; //使用System.Collections.Generic; 运用系统反思; 名称空间FixUtil { 班级计划 { 静态void Main(字符串[]参数) { FIXMessage fix=新的FIXMessage(); fix.top1=“FIXML”; fix.top2=“fixml

Changetype不能更改为类型<代码>创建(类型)

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Xml.Linq;
//使用System.Collections.Generic;
运用系统反思;
名称空间FixUtil
{
班级计划
{
静态void Main(字符串[]参数)
{
FIXMessage fix=新的FIXMessage();
fix.top1=“FIXML”;
fix.top2=“fixmlmmessage”;
XMLUtil go=新的XMLUtil(修复);
XDocument xdoc=新XDocument(新XDeclaration(“1.0”、“UTF-8”、“是”)、新XElement(“FIXML”);
XElement top2=新XElement(“FIXMLMessage”,null);
XElement Header=go.createHeader();
top2.添加(标题);
XElement ApplicationMessage=go.createApplicationMessage();
top2.Add(ApplicationMessage);
XElement order_元素=go.createOrder();
ApplicationMessage.Add(order\u元素);
//Order.Add(go.createClOrderID(“1968”));
订单\实例=新订单();
order_instance.CIOrdID=“1968”;
order_instance.instrument=新仪器();
订单_instance.instrument.Symbol=“1171”;
创建(order\u元素、order\u实例);
//创建(订单元素);
xdoc.Root.Add(top2);
xdoc.Save(“./FIX.xml”);
while(true)
{ }
}
//受保护的静态void create(XElement root),其中T:new()
受保护的静态void create(XElement根,T实例),其中T:class
{
//MethodInfo[]methodInfos=typeof(T).GetMethods(BindingFlags.Public | BindingFlags.Static);
if(typeof(T).GetProperties().Count()==0)
回来
foreach(typeof(T).GetProperties()中的PropertyInfo memberinfo)
{
字符串类型=memberinfo.PropertyType.ToString().Split('.')[1].ToString();
畸形儿;
如果(类型==“字符串”)
{
Add(new-XElement(memberinfo.Name,memberinfo.GetValue(instance,null));
//添加(新元素(memberinfo.Name,null));
}
其他的
{
child=new-XElement(memberinfo.Name,null);
root.Add(子级);
创建(子项,memberinfo.GetType());
//创建(子对象、实例);
//创造(儿童);
}
Console.WriteLine(memberinfo.Name+“”
+memberinfo.PropertyType.ToString()
//+memberinfo.GetValue(实例,null).ToString()+“”
+memberinfo.PropertyType);
}
回来
}
受保护的静态void创建(XElement根,动态实例)
{
//返回null;
//Add(new-XElement(instance,GetValue(instance,null));
//子元素=新元素(根,空);
//创造(儿童);
//MethodInfo[]methodInfos=instance.GetMethods(BindingFlags.Public | BindingFlags.Static);
if(instance.GetProperties().Count()==0)
回来
foreach(实例.GetProperties()中的PropertyInfo memberinfo)
{
字符串类型=memberinfo.PropertyType.ToString().Split('.')[1].ToString();
畸形儿;
如果(类型==“字符串”)
{
//Add(new-XElement(memberinfo.Name,memberinfo.GetValue(instance,null));
添加(新元素(memberinfo.Name,null));
}
其他的
{
child=new-XElement(memberinfo.Name,null);
root.Add(子级);
创建(子项,memberinfo.GetType());
//创建(子对象、实例);
//创造(儿童);
}
Console.WriteLine(memberinfo.Name+“”
+memberinfo.PropertyType.ToString()
//+memberinfo.GetValue(实例,null).ToString()+“”
+memberinfo.PropertyType);
}
回来
}
}
}
转换。ChangeType(类型)
不是类型参数。这是函数调用。必须在尖括号内提供文字类型或泛型类型参数。不能使用
系统。键入
引用,当然也不能使用
对象
引用。

调用此方法:

create<Person>(xmlDoc);

并提供不接受类型的create函数重载。它更干净,因为现在您不必知道将返回什么类型。

如果您需要的类型不是T,那么您尝试对泛型执行的操作是不可能的

您请求的值必须是泛型值或编译时已知的值

这意味着您的唯一选择是:

create<T>(root);
create(root);
或:

create(root);

或者添加其他泛型参数。

在Create中,我应该向T传递什么,因为可能有另一个类类型传递到classi中。我已经获得了成员的类型名称,但不知道如何将其更改为传递到的类型。我的问题是将成员的类型名称更改为传递到Create(type)编译错误的T中的类型,call、DECRIMENT和new object expression可以用作语句。您能告诉我们调用代码吗?您只能从定义create的类或其一个子类中调用此方法。已编辑,请查看哪行代码上面的代码导致编译错误?以及,是否确实要从类型中删除命名空间?
dynamic obj = create(xmlDoc);
create<T>(root);
create<PureTypeName>(root);