从反射 ParamInfo[] 创建匿名类型
当匿名类型属性是函数参数时,我想在函数内创建匿名类型。
例如,对于函数: private bool CreatePerson(string FirstName, string LasName, int Age, int height);
我将有一个匿名类型,其属性为:FirstName、LasName、Age 和 height。 函数参数的值将是匿名类型属性的值。
private bool CreatePerson(string FirstName, string LasName, int Age, int height)
{
// Get this method parameters
MethodBase currentMethod = MethodBase.GetCurrentMethod();
ParameterInfo[] parametersInfo = currentMethod.GetParameters();
// create an object of the parameters from the function.
foreach (ParameterInfo paramInfo in parametersInfo)
{
// add a property with the name of the parameter to an anonymous object and insert its value to the property.
// WHAT DO I DO HERE?
....
}
return true;
}
i want to create an anonymous type inside a function, when the anonymous type properties are the function parameters.
for example, for the function:
private bool CreatePerson(string FirstName, string LasName, int Age, int height);
i will have an anonymous type with the properties: FirstName,LasName,Age and height.
and the values of the function parameters will be the values of the anonymous type properties.
private bool CreatePerson(string FirstName, string LasName, int Age, int height)
{
// Get this method parameters
MethodBase currentMethod = MethodBase.GetCurrentMethod();
ParameterInfo[] parametersInfo = currentMethod.GetParameters();
// create an object of the parameters from the function.
foreach (ParameterInfo paramInfo in parametersInfo)
{
// add a property with the name of the parameter to an anonymous object and insert its value to the property.
// WHAT DO I DO HERE?
....
}
return true;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果我理解正确并且您想在运行时定义属性,这是不可能的。尽管在匿名类型中,您可以创建自己定义的类型,然后通过赋值,但属性的名称必须在编译时已知。
事实上,该类型对于您来说是匿名的,但对于 CLR 来说却不是。如果您查看 ildasm.exe 或 Reflector 中的程序集,您将看到这些具有奇怪名称的匿名类型,其名称中始终包含
<>
。C# 的动态可能会在这里有所帮助,但据我所知,它们有助于与对象通信,这些对象是我们没有类型信息的对象,而不是创建对象 - 但我可能有一种方法不知道。
If I understood correctly and you want to define the properties at runtime, this is not possible. Although in anonymous types you may be able to create types that you define there and then by assigning values, the name of the properties must be known at compile time.
In fact, the type is anonymous to you but not to the CLR. If you look at the assembly in ildasm.exe or reflector, you will see these anonymous types with strange names always having
<>
in their names.C#'s dynamic might be able to help here but as far as I know, they help with communicating with objects that are that we do not have type information for, not creating - but there might be a way that I do not know.
您不能使用“Linq to DataSet”
Field(String Name)
设计模式吗?事实上为什么不直接使用 DataTable。编译器不需要知道该字段是否存在,只需要知道它是什么类型即可保证类型安全。这样做的原因之一是实现某种类型的解析器来生成过滤器,或动态配置字段名称。Could you not use the "Linq to DataSet"
Field<T>(String Name)
design pattern? In fact why not just use a DataTable. The compiler does not need to know that the field exists, only what type it is to be type safe. One reason to do this would be to implement some type of parser to generate filters, or to configure field names dynamically.