C# 如何将此字符串列表强制转换为对象
如果我有这个字符串列表:C# 如何将此字符串列表强制转换为对象,c#,C#,如果我有这个字符串列表: string myObjectString = "MyObject, SetWidth, int, 10, 0, 1"; 其中: - MyObject: the object class - SetWidth: the property of the object - int: type of the SetWidth is int - 10: default value - 0: object order - 1: property order 那么我如何构建这样
string myObjectString = "MyObject, SetWidth, int, 10, 0, 1";
其中:
- MyObject: the object class
- SetWidth: the property of the object
- int: type of the SetWidth is int
- 10: default value
- 0: object order
- 1: property order
那么我如何构建这样一个对象:
[ObjectOrder(0)]
public class MyObject:
{
private int _SetWidth = 10;
[PropertyOrder(1)]
public int SetWidth
{
set{_SetWidth=value;}
get{return _SetWidth;}
}
}
Object myObject = ConstructAnObject(myObjectString);
所以,我想要这样的东西:
[ObjectOrder(0)]
public class MyObject:
{
private int _SetWidth = 10;
[PropertyOrder(1)]
public int SetWidth
{
set{_SetWidth=value;}
get{return _SetWidth;}
}
}
Object myObject = ConstructAnObject(myObjectString);
myObject是myObject的一个实例。在C语言中可能吗
提前谢谢 我认为最好使用对象序列化/反序列化,而不是创建基本上需要做同样事情的自定义方法 更多信息,请访问:
我认为最好使用对象序列化/反序列化,而不是创建基本上需要做相同事情的自定义方法 更多信息,请访问:
假设您需要生成新类型,有两种可能的方法: 使用 使用 我认为更简单的解决方案是CodeDom提供者。所需的只是在内存中以字符串形式生成源代码,然后编译代码并使用实例实例化一个新实例。这是我刚找到的一个不错的。 我认为CodeDom提供程序更简单的原因是它的设置更短—不需要生成动态模块和程序集,然后使用类型生成器和成员生成器。此外,生成getter和setter主体不需要使用IL。
反射发射的一个优点是性能—即使在使用了其中一种类型之后,动态模块也可以向自身添加更多类型。CodeDom提供程序要求一次创建所有类型,否则每次都会创建一个新程序集。假设需要生成新类型,有两种可能的方法: 使用 使用 我认为更简单的解决方案是CodeDom提供者。所需的只是在内存中以字符串形式生成源代码,然后编译代码并使用实例实例化一个新实例。这是我刚找到的一个不错的。 我认为CodeDom提供程序更简单的原因是它的设置更短—不需要生成动态模块和程序集,然后使用类型生成器和成员生成器。此外,生成getter和setter主体不需要使用IL。
反射发射的一个优点是性能—即使在使用了其中一种类型之后,动态模块也可以向自身添加更多类型。CodeDom提供程序要求一次创建所有类型,否则每次都会创建一个新程序集。如果使用C 4.0,则可以使用新的动态功能
如果使用C4.0,则可以使用新的动态功能
我不太明白你为什么需要ObjectOrder和PropertyOrder。。。一旦你有了他们的名字,你可能就不需要他们了,至少对于反序列化来说 或者请告知他们的角色是什么 您完全可以通过反射来实现: 使用myString.Split按逗号拆分字符串 使用反射在应用程序中查找对象: 查找name=splittedString[0]的类型枚举域内的所有程序集以及每个程序集内的所有类型; 实例化使用Activator.CreateInstance找到的类型 使用objectType.GetProperty按名称查找属性 使用propertyInfo.SetValue设置属性值 返回对象
我不太明白你为什么需要ObjectOrder和PropertyOrder。。。一旦你有了他们的名字,你可能就不需要他们了,至少对于反序列化来说 或者请告知他们的角色是什么 您完全可以通过反射来实现: 使用myString.Split按逗号拆分字符串 使用反射在应用程序中查找对象: 查找name=splittedString[0]的类型枚举域内的所有程序集以及每个程序集内的所有类型; 实例化使用Activator.CreateInstance找到的类型 使用objectType.GetProperty按名称查找属性 使用propertyInfo.SetValue设置属性值 返回对象
下面是一些快速而肮脏的代码,让您开始:
string myObjectString = "MyObject, SetWidth, int, 10, 0, 1";
var info = myObjectString.Split(',');
string objectName = info[0].Trim();
string propertyName = info[1].Trim();
string defaultValue = info[3].Trim();
//find the type
Type objectType = Assembly.GetExecutingAssembly().GetTypes().Where(t=>t.Name.EndsWith(objectName)).Single();//might want to redirect to proper assembly
//create an instance
object theObject = Activator.CreateInstance(objectType);
//set the property
PropertyInfo pi = objectType.GetProperty(propertyName);
object valueToBeSet = Convert.ChangeType(defaultValue, pi.PropertyType);
pi.SetValue(theObject, valueToBeSet, null);
return theObject;
这将找到MyObject,创建一个具有适当propertytype的对象,并设置匹配的属性。以下是一些快速而肮脏的代码,帮助您入门:
string myObjectString = "MyObject, SetWidth, int, 10, 0, 1";
var info = myObjectString.Split(',');
string objectName = info[0].Trim();
string propertyName = info[1].Trim();
string defaultValue = info[3].Trim();
//find the type
Type objectType = Assembly.GetExecutingAssembly().GetTypes().Where(t=>t.Name.EndsWith(objectName)).Single();//might want to redirect to proper assembly
//create an instance
object theObject = Activator.CreateInstance(objectType);
//set the property
PropertyInfo pi = objectType.GetProperty(propertyName);
object valueToBeSet = Convert.ChangeType(defaultValue, pi.PropertyType);
pi.SetValue(theObject, valueToBeSet, null);
return theObject;
这将找到MyObject,创建一个具有适当属性类型的对象,并设置匹配属性。等等。。。您想一次生成类并实例化它的实例吗?或者您已经定义了类,只想实例化一个实例?等等。。。您想一次生成类并实例化它的实例吗?或者您已经定义了类,只想实例化一个实例?有了这个声明:dynamic myObj,我想我做不到:myObj.MyObject。如果我声明:dynamicmyobj=新对象;那么,我无法使用以下声明执行myObj.MyObject=objectproperties[0]:dynamic myObj,我想我无法执行:myObj.MyObject。如果我声明:dynamicmyobj=新对象;那么我就不能做myObj.MyObject=objectproperties[0]这看起来不错,我如何添加:[ObjectOrder0]和[PropertyOrder1]?提前谢谢。我不确定,据我所知,属性是在类型上定义的,而不是在
在单个实例上。这看起来不错,如何添加:[ObjectOrder0]和[PropertyOrder1]?提前谢谢。我不确定,据我所知,属性是在类型上定义的,而不是在单个实例上定义的。