Reflection and Generics.

R

Rathish P S

Hi friends,
I am trying with Reflection and Generics in C#. But I get
confused with some of these new features. I am trying to get the type
of generic parameter with the type.Assembly.GetType(name,false,false)
method. It is return as null. Can anybody tell can help me to find the
basic reason of this issue?
this is the sample code that I used.

using System;
using System.Collections.Generic;
using System.Text;
using System.CodeDom.Compiler;
using System.Reflection;
using System.Diagnostics;

namespace GenericReflectionApp
{
class Program
{
static void Main(string[] args)
{
string assemblyPath = GetValidAssembly(args);
assemblyPath = System.Environment.CurrentDirectory;
assemblyPath = assemblyPath + @"\GenericsLib.dll";
Assembly GenericLibAssembly = Assembly.LoadFrom(assemblyPath);
Type[] allType = GenericLibAssembly.GetTypes();
foreach(Type type in allType)
{
if (type.IsGenericType)
{
MethodInfo[] methodInfo = type.GetMethods();
foreach (MethodInfo mInfo in methodInfo)
{
CheckMethodParametersType(mInfo);
CheckLocalVariableType(mInfo);
}
}
}
}

private static void CheckLocalVariableType(MethodInfo mInfo)
{
if (mInfo.GetMethodBody() != null)
{
IList<LocalVariableInfo> localInfo =
mInfo.GetMethodBody().LocalVariables;
foreach (LocalVariableInfo lInfo in localInfo)
{
Type ltype = lInfo.LocalType;
if (ltype.IsArray)
{
if (ltype.ContainsGenericParameters)
{
Type arrayType = ltype.Assembly.GetType(ltype.Name, false,
false);
Debug.Assert(arrayType != null, "Generic Array Type Null (Local
Variable)");
}
else
{
Type arrayType = ltype.Assembly.GetType(ltype.FullName, false,
false);
Debug.Assert(arrayType != null, "Type Array Null (Local
Variable)");
}
}
}
}
}

private static void CheckMethodParametersType(MethodInfo mInfo)
{
ParameterInfo[] parameterInfo = mInfo.GetParameters();
foreach (ParameterInfo pInfo in parameterInfo)
{
Type ptype = pInfo.ParameterType;
if (ptype.IsArray)
{
if (ptype.ContainsGenericParameters)
{
Type arrayType = ptype.Assembly.GetType(ptype.Name, false,
false);
Debug.Assert(arrayType != null, "Generic Array Type Null
(Parameter)");
}
else
{
Type arrayType = ptype.Assembly.GetType(ptype.FullName, false,
false);
Debug.Assert(arrayType != null, "Type Array Null (Parameter)");
}
}
}
}

private static string GetValidAssembly(string[] sAssem)
{
string sAssemName;

if (0 == sAssem.Length)
{
// get this assembly
System.Diagnostics.Process pr =
System.Diagnostics.Process.GetCurrentProcess();
sAssemName = pr.ProcessName + ".exe";
}
else
{
// it seems user has supplied some assembly name..ok take that
sAssemName = sAssem[0];
}

return sAssemName;
}
}
}


Thanks!
Rathish P S.
 
M

Mattias Sjögren

I am trying to get the type
of generic parameter with the type.Assembly.GetType(name,false,false)
method. It is return as null.

Why? You already have the parameter type in ptype. What's the point of
getting its name as a string and then turning it back into a Type?

If it's the array's generic element type you're after, use
ptype.GetElementType().


Mattias
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top