I am trying to use reflection to query the public interface of a class library, then produce classes and methods from info in the query.
The problem is that the generated source code won't compile:
-
The original class library has a function parameter being "List<Employee>", but the generated source code has "System.Collections.Generic.List`1[Employee]". It is generated by Type.ToString().
-
The original class library has "Dictionary<string, Employee>", but the generated code has "System.Collections.Generic.Dictionary`2[string, Employee]".
Here is the reflection code to generate source code:
foreach (MethodInfo methodInfo in type.GetMethods())
{
if (!methodInfo.IsStatic)
continue;
Console.Write($" {(methodInfo.IsStatic ? "static" : "")} {methodInfo.ReturnType} {methodInfo.Name} (");
ParameterInfo[] aParams = methodInfo.GetParameters();
for (int i = 0; i < aParams.Length; i++)
{
ParameterInfo param = aParams[i];
if (i > 0)
Console.Write(", ");
string strRefOrOut;
if (param.ParameterType.IsByRef)
{
if (param.IsOut)
strRefOrOut = "out ";
else
strRefOrOut = "ref ";
}
else
strRefOrOut = "";
Console.Write($"{ strRefOrOut }{ param.ParameterType.ToString() } {param.Name}");
}
Console.WriteLine(");");
}
It produces the following source code that cannot be compiled:
static System.Void Test1 (System.Collections.Generic.List`1[Employee] employees);
static System.Void Test (System.Collections.Generic.Dictionary`2[System.String, Employee] dict);
I want it to be
static System.Void Test1 (List<Employee> employees);
static System.Void Test (Dictionary<System.String, Employee> dict);
How do I get the desired outcome from the Type?
I don't want to do ugly "if/else if/else if/else" string manipulations to convert them, because if I forget to include a type of collection then it will break.
Is there an elegant way of automatically get the former, such as Type.ProperName?
Aucun commentaire:
Enregistrer un commentaire