473,473 Members | 4,257 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reflection and Generics.

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.

Nov 17 '05 #1
1 2264
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Joanna Carter \(TeamB\) | last post by:
Hi folks I have a Generic Value Type and I want to detect when the internal value changes. /////////////////////////////// public delegate void ValueTypeValidationHandler<T>(T oldValue, T...
7
by: Jim Robertson | last post by:
Is it possible to instantiate a generic class when the parametrized type isn't known until runtime? For example, in Indigo you can deploy a web service with something like... ServiceHost<Foo> sh =...
2
by: Marc | last post by:
Given a class 'Invoice' with a property 'public IMyColl<IInvoiceLine> InvoiceLines' where 'IMyColl<T> : IList<T>' i would like to detect by reflection that 'InvoiceLines' is a...
4
by: Dov Tendler | last post by:
Hi All, I would like to get MethodInfo of current instantiated generic method call. Consider the following function call: C.f<int>(5); Where f is defined as follows: class C {
1
by: uttara | last post by:
I have a generic collection which I am using in classes to store a collection of embedded objects. Class Employee: IEntity { Private string mName; Private int mEmployeeID; …. Private...
2
by: Wiktor Zychla [C# MVP] | last post by:
Could anyone confirm/deny that following is a bug (or at least an "unexpected behaviour")? If this is not a bug, I would be glad for a short explanation or a workaround. Issue: A generic...
0
by: Konrad Kaczanowski | last post by:
Hi all, I'm creating code generator for wrappers of some c# classes. With the introduction of c# 2.0 and generics the following problem arises. When encountering generic types anywhere inside the...
4
by: bowser | last post by:
Hello, I'm new to C# but so far I didn't find big problems, except for Reflection. I coudn't find any good material in internet where to study it. If anyone knows about some documents, blogs or...
7
by: =?Utf-8?B?TXJOb2JvZHk=?= | last post by:
Say I have a class that has a generics List as follows: public List<MyClassmyClassList = new List<MyClass>(); and I want to create another class which tries to add an element of MyClass to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.