我想創建一個匿名類型內的函數,當匿名類型屬性是函數參數。從反射創建一個匿名類型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