473,805 Members | 2,034 Online
Bytes | Software Development & Data Engineering Community
+ 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.G etType(name,fal se,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.Collecti ons.Generic;
using System.Text;
using System.CodeDom. Compiler;
using System.Reflecti on;
using System.Diagnost ics;

namespace GenericReflecti onApp
{
class Program
{
static void Main(string[] args)
{
string assemblyPath = GetValidAssembl y(args);
assemblyPath = System.Environm ent.CurrentDire ctory;
assemblyPath = assemblyPath + @"\GenericsLib. dll";
Assembly GenericLibAssem bly = Assembly.LoadFr om(assemblyPath );
Type[] allType = GenericLibAssem bly.GetTypes();
foreach(Type type in allType)
{
if (type.IsGeneric Type)
{
MethodInfo[] methodInfo = type.GetMethods ();
foreach (MethodInfo mInfo in methodInfo)
{
CheckMethodPara metersType(mInf o);
CheckLocalVaria bleType(mInfo);
}
}
}
}

private static void CheckLocalVaria bleType(MethodI nfo mInfo)
{
if (mInfo.GetMetho dBody() != null)
{
IList<LocalVari ableInfo> localInfo =
mInfo.GetMethod Body().LocalVar iables;
foreach (LocalVariableI nfo lInfo in localInfo)
{
Type ltype = lInfo.LocalType ;
if (ltype.IsArray)
{
if (ltype.Contains GenericParamete rs)
{
Type arrayType = ltype.Assembly. GetType(ltype.N ame, false,
false);
Debug.Assert(ar rayType != null, "Generic Array Type Null (Local
Variable)");
}
else
{
Type arrayType = ltype.Assembly. GetType(ltype.F ullName, false,
false);
Debug.Assert(ar rayType != null, "Type Array Null (Local
Variable)");
}
}
}
}
}

private static void CheckMethodPara metersType(Meth odInfo mInfo)
{
ParameterInfo[] parameterInfo = mInfo.GetParame ters();
foreach (ParameterInfo pInfo in parameterInfo)
{
Type ptype = pInfo.Parameter Type;
if (ptype.IsArray)
{
if (ptype.Contains GenericParamete rs)
{
Type arrayType = ptype.Assembly. GetType(ptype.N ame, false,
false);
Debug.Assert(ar rayType != null, "Generic Array Type Null
(Parameter)");
}
else
{
Type arrayType = ptype.Assembly. GetType(ptype.F ullName, false,
false);
Debug.Assert(ar rayType != null, "Type Array Null (Parameter)");
}
}
}
}

private static string GetValidAssembl y(string[] sAssem)
{
string sAssemName;

if (0 == sAssem.Length)
{
// get this assembly
System.Diagnost ics.Process pr =
System.Diagnost ics.Process.Get CurrentProcess( );
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 2286
I am trying to get the type
of generic parameter with the type.Assembly.G etType(name,fal se,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.GetElemen tType().
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
2295
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 newValue); public struct TestType<T> {
7
4327
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 = new ServiceHost<Foo>(myURI); sh.Open(); What if the type "Foo" isn't known until runtime? Can I use reflection to create a Type object corresponding to the type ServiceHost<Foo>, then use Activator.CreateInstance()? How would I do this? Do I use...
2
2877
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 'System.Collection.Generic.IList'. When performing something like: 'if (typeof(IList<>).IsAssignableFrom(propertyInfo.Type))' where 'propertyInfo' obviously refers to the 'InvoiceLines', the result is always 'false' because indeed 'IList<object>' and...
4
2459
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
6096
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 GenericCollection<Address> mAddresses;
2
4245
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 class, Base, with a constraint on the generic parameter. A generic class, RelTable, with a constraint on two generic parameters. A reflection code that just enumerates types, methods and methods'
0
1215
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 assembly, for example a generic method parameter declared as: System.Collections.Generic.List<int> the full name of that parameter type returned by reflection is :
4
1823
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 stuff, please let me know. My problem is that I have to create a new instance of a class, given a type and a name (object's id) as parameters. The constructor of such type has no parameters.
7
6357
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 that list, but it is not explicitly creating an instance of MyClass, but instead using the Activator to create an instance based on it's type name. How can I accomplish this in C# using reflection?
0
9716
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9596
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10604
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10356
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10361
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9179
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
4316
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3839
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3006
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.