我想创建一个匿名类型内的函数,当匿名类型属性是函数参数。从反射创建一个匿名类型ParamInfo []
例如,对于函数: private bool CreatePerson(string FirstName,string LasName,int Age,int height);
我将有一个匿名类型的属性:名字,LasName,年龄和身高。 并且函数参数的值将是匿名类型属性的值。
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;
}
你想创建一个具有已知字段名称和类型的匿名类型,为什么你需要反射?新{FirstName = FirstName,...}会很好吗? – SoftMemes 2010-10-24 13:06:16
他希望在运行时定义 - 我相信。 – Aliostad 2010-10-24 13:10:03
@Aliostad - 但描述只是说_values_应该来自参数,参数/属性的名称已经存在... – SoftMemes 2010-10-24 13:12:01