I am trying to create a tool which invokes all of the functions within a library through reflection, for this i need to get the method information and parameters information. I was able to get these parameter info (with default values like if it is int =0, string = empty, float = 0, enum = getting a type values and 0 index value , if a parameter is of some type(isClass) then null.).
In this case i have one problem there are some functions which uses parameters List<int> List<enum>, list<type>
now while creating a default values for these parameters is bit challenging for me, Can anyone help me on this.
1) List or any numerical list default values should be List<int> defaultValueList = new List<int>(){0};
2) For list<Enum> enumDefault = new List<Enum>(){ Enum.Getvalues(0)}
3)for List class_Default = get type , create instance , add it to one list after initializing( it should not be null that is the only objective that i have in this program.
Any help would be appreciated
Some of the code sample (Please note that NAMES defined above has nothing to do with the following example )
private object[] GetParameterInfoDefault(ParameterInfo[] pInfo)
{
int count = pInfo.Count();
object[] objDefaultValues = new object[count] ;
try
{
int i = 0;
foreach (ParameterInfo info in pInfo)
{
objDefaultValues[i] = GetDefaultValurForTypes(info);
i++;
}
}
catch (Exception ex)
{
}
return objDefaultValues;
}
EDIT
I am trying to create default values here in this function for each parameter but for List i am not sure what has to be done i.e first part of the function
private object GetDefaultValurForTypes(ParameterInfo info)
{
object objectDefaultValue = new object();
try
{
if (info.ParameterType.UnderlyingSystemType.Name.Equals("List`1"))
{
List<object> objList ;
Type[] tempTypes = info.ParameterType.GetGenericArguments();
foreach (Type tempT in tempTypes)
{
if (tempT.UnderlyingSystemType == typeof(int))
{
objectDefaultValue = (tempT.UnderlyingSystemType.GetType()) ;
objectDefaultValue = 1;
}
else if (tempT.UnderlyingSystemType.IsEnum)
{
Array arr = tempT.UnderlyingSystemType.GetEnumValues();
objectDefaultValue = arr.GetValue(0);
}
else if (tempT.UnderlyingSystemType.IsClass)
{
objectDefaultValue = Activator.CreateInstance(tempT);
//FieldInfo [] fldInfo = tempT.UnderlyingSystemType.GetFields();
//object [] fldInfoDefaultValues = new object[fldInfo.Count()];
//int i = 0;
//foreach (FieldInfo fldinf in fldInfo)
//{
// fldInfoDefaultValues[i] = fldInfo.get
//}
}
}
}
else if (info.ParameterType.BaseType == typeof(Enum))
{
//object underlyingValue = Convert.ChangeType(Enum.GetUnderlyingType(value.GetType()));
Array arrayValue = info.ParameterType.GetEnumValues();
objectDefaultValue = arrayValue.GetValue(0);
//var value = propertyInfo.GetValue(obj); // this return TestOne or TestTwo
//var enumValue = Convert.ChangeType(value, typeof(int)); // this return 3 or 4
}
//else if( info.ParameterType.BaseType == System
//{
//}
//else if (info.ParameterType.BaseType == typeof(int) || info.ParameterType.BaseType == typeof(float) || info.ParameterType.BaseType == typeof(double))
// objectDefaultValue = 0;
else if (info.ParameterType == typeof(bool))
objectDefaultValue = false;
else if (info.ParameterType == typeof(int))
objectDefaultValue = 1;
else if (info.ParameterType == typeof(string))
objectDefaultValue = string.Empty;
//else if(info.ParameterType. == typeof(
else
objectDefaultValue = 0;
}
catch (Exception ex)
{
}
return objectDefaultValue;
}
Aucun commentaire:
Enregistrer un commentaire