473,670 Members | 2,407 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

meta-generics

suppose I have a class

class MyContainter {
List<something1 list1;
List<something2 list2;
...
List<somethingn listn;

othertype1 otherfield1;
...
othertypek otherfieldk;
}

as you can see there are plenty of generics instantiated with different
types.

what I wish is to use reflection to get a list of fields that are of type
List<anything>:

foreach ( FieldInfo fi in typeof(MyContai nter).GetFields () )
{
object val = fi.GetValue( TheInstance );

if ( ???? )
{
}
}

what is the best approach to fill the ???? gap so that I have only all these
List<Tfields passed into the if block?

solutions I think about are:
a) matching the type by name - however this is ugly
b) additional attribute on all List<Tfields - seems unnecessary

what I would like is to use pure reflection. something like:

if ( val is List ) { }
if ( val.GetType().I sSubclassOf( ... ) ) { }

however, none of my tries compile since List<is a generic type and cannot
be used without instantiation of the generic argument. in other words, I
cannot make generics generic on another level of abstraction.

is it possible at all? right now I tend to think that this is impossible.

[I should mention that in the real-life application there are no List<T>
fields but my_own_generic_ class<T,Kso any List<Tspecific solutions are
not acceptable]

Thanks in advance,
Wiktor Zychla

Jul 7 '06 #1
5 1646
"Wiktor Zychla [C# MVP]" <wz*****@nospm. ii.uni.wroc.pl. nospmwrote:
class MyContainter {
List<something1 list1;
what I wish is to use reflection to get a list of fields that are of type
List<anything>:

foreach ( FieldInfo fi in typeof(MyContai nter).GetFields () )
{
Type t = fi.FieldType;

if (t.IsGenericTyp e
&& t.GetGenericTyp eDefinition() == typeof(List<>)
&& t.GetGenericArg uments()[0] == typeof(anything ))
// ...

-- Barry

--
http://barrkel.blogspot.com/
Jul 7 '06 #2
Barry Kelly <ba***********@ gmail.comwrote:
"Wiktor Zychla [C# MVP]" <wz*****@nospm. ii.uni.wroc.pl. nospmwrote:
class MyContainter {
List<something1 list1;
what I wish is to use reflection to get a list of fields that are of type
List<anything>:

foreach ( FieldInfo fi in typeof(MyContai nter).GetFields () )
{

Type t = fi.FieldType;

if (t.IsGenericTyp e
&& t.GetGenericTyp eDefinition() == typeof(List<>)
&& t.GetGenericArg uments()[0] == typeof(anything ))
// ...
I should point out that this is most suitable if you don't already know
that you're dealing with 'anything'. If you do statically know the type,
then you can simply do:

if (fi.FieldType == typeof(List<any thing>))

-- Barry

--
http://barrkel.blogspot.com/
Jul 7 '06 #3
I am not sure I follow the question.

You have an instance (so you would be dealing with the closed type of the
generic from getvalue)

could you use Type.GetGeneric TypeDefinition( ) to get the open version of the
generic type.then compare this to the type you are after?

If I am understanding you correctly something like this??
public class Foo<T{
public List<Tbar;
public List<Tbar2;
public int test;
public float test2;
public Foo() {
bar = new List<T>();
bar2 = new List<T>();
}

}

static void CheckForFieldTy pe(object o, Type ToFind) {
foreach (FieldInfo fi in o.GetType().Get Fields()) {
if (fi.FieldType.I sGenericType &&
fi.FieldType.Ge tGenericTypeDef inition() == ToFind) {
Console.WriteLi ne("Found " + fi.Name);
}
}
}

static void Main(string[] args) {
Foo<intfoo = new Foo<int>();
CheckForFieldTy pe(foo, typeof(List<>)) ;
}
Cheers,

Greg Young
MVP - C#
http://codebetter.com/blogs/gregyoung

"Wiktor Zychla [C# MVP]" <wz*****@nospm. ii.uni.wroc.pl. nospmwrote in
message news:Oa******** *****@TK2MSFTNG P05.phx.gbl...
suppose I have a class

class MyContainter {
List<something1 list1;
List<something2 list2;
...
List<somethingn listn;

othertype1 otherfield1;
...
othertypek otherfieldk;
}

as you can see there are plenty of generics instantiated with different
types.

what I wish is to use reflection to get a list of fields that are of type
List<anything>:

foreach ( FieldInfo fi in typeof(MyContai nter).GetFields () )
{
object val = fi.GetValue( TheInstance );

if ( ???? )
{
}
}

what is the best approach to fill the ???? gap so that I have only all
these List<Tfields passed into the if block?

solutions I think about are:
a) matching the type by name - however this is ugly
b) additional attribute on all List<Tfields - seems unnecessary

what I would like is to use pure reflection. something like:

if ( val is List ) { }
if ( val.GetType().I sSubclassOf( ... ) ) { }

however, none of my tries compile since List<is a generic type and
cannot be used without instantiation of the generic argument. in other
words, I cannot make generics generic on another level of abstraction.

is it possible at all? right now I tend to think that this is impossible.

[I should mention that in the real-life application there are no List<T>
fields but my_own_generic_ class<T,Kso any List<Tspecific solutions are
not acceptable]

Thanks in advance,
Wiktor Zychla

Jul 7 '06 #4
too quick for me .. :( was typing up that quick example

Cheers,

Greg
"Barry Kelly" <ba***********@ gmail.comwrote in message
news:or******** *************** *********@4ax.c om...
Barry Kelly <ba***********@ gmail.comwrote:
>"Wiktor Zychla [C# MVP]" <wz*****@nospm. ii.uni.wroc.pl. nospmwrote:
class MyContainter {
List<something1 list1;
what I wish is to use reflection to get a list of fields that are of
type
List<anything>:

foreach ( FieldInfo fi in typeof(MyContai nter).GetFields () )
{

Type t = fi.FieldType;

if (t.IsGenericTyp e
&& t.GetGenericTyp eDefinition() == typeof(List<>)
&& t.GetGenericArg uments()[0] == typeof(anything ))
// ...

I should point out that this is most suitable if you don't already know
that you're dealing with 'anything'. If you do statically know the type,
then you can simply do:

if (fi.FieldType == typeof(List<any thing>))

-- Barry

--
http://barrkel.blogspot.com/

Jul 7 '06 #5
too quick for me .. :( was typing up that quick example

big thanks for both of you. what I missed is the possibility of having an
"open" generic type with:

typeof( generictype<)

this solves my problem completely.

regards,
Wiktor

Jul 7 '06 #6

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

Similar topics

1
4008
by: Cezary | last post by:
Hello. I was read PHP manual, but i'm not sure yet. Here is my meta tags in html: <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=ISO-8859-2"> <META HTTP-EQUIV="Expires" CONTENT="0"> <META HTTP-EQUIV="Cache-Control" CONTENT="no-store, no-cache, must-revalidate, max-age=0, post-check=0, pre-check=0"> <META HTTP-EQUIV="Pragma" CONTENT="no-cache">
4
3720
by: Brian | last post by:
Hi, I'm trying to use standard meta tags in an xsl doc and using cocoon as my processor. The problem is that cocoon changes for example: <meta name="keywords" content="test, test, test" /> to <meta content="test, test, test" name="keywords"> I hope that makes sense. The problem is that I am running some adds on my
1
2597
by: Darren Blackley | last post by:
Hi there I have documents that I want to automatically add additional meta tags to. The documents already have some meta tags and I want to keep them all together, so I want to add my new meta tags to the end of the existing ones... can someone help me out with a script to do this... example below. <head> <title>The Document Title</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> <meta name="discription"...
19
3846
by: Christian Hvid | last post by:
Hello groups. I have a series of applet computer games on my homepage: http://vredungmand.dk/games/erik-spillet/index.html http://vredungmand.dk/games/nohats/index.html http://vredungmand.dk/games/platfoot/index.html http://vredungmand.dk/games/minorbug/index.html http://vredungmand.dk/games/timbuktu/index.html http://vredungmand.dk/games/taleban/index.html
24
3540
by: Day Bird Loft | last post by:
Web Authoring | Meta-Tags The first thing to understand in regard to Meta Tags is the three most important tags placed in the head of your html documents. They are the title, description, and keyword meta-tags. If you are missing any of these meta-tags you are missing the boat. If you use the following meta-tag formula, and you are not trying to deceive the spiders, I guarantee you will succeed in increasing your placement in the...
3
32898
by: J1C | last post by:
How can I programatically add meta tags with javascript?
5
12086
by: RodneyDunes | last post by:
My site did validate and now it doesn't. The error I get is the following: document type does not allow element "META" here ....nt-type" content="text/html;charset=iso-8859-1"> Can someone please tell me what I need to change?These are the Meta tags: </head> <meta http-equiv="Content-type" content="text/html;charset=iso-8859-1"> <META http-equiv="PICS-Label" content=(PICS-1.1 "http://www.classify.org/safesurf/" L gen true for
1
1953
by: Maziar Aflatoun | last post by:
Hi everyone, My goal is to modify the contents of my meta tag (html refresh). However, my code adds a new instance of the meta tag at the bottom of the page. Is there a way to modify it instead of adding a new one? <head> <title>WebForm1</title> <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#">
4
2296
by: clintonG | last post by:
Anybody know how to dynamically write the meta tags using code so they are formatted on a separate line in the HTML source? Preferred or optimal framework classes that may be used in this regard? <meta... /> <meta... /> <meta... /> <%= Clinton Gallagher
16
2499
by: Edward | last post by:
Hi All, I am having huge problems with a very simple dotnet framework web page (www.gbab.net/ztest3.aspx) , it does NOT render correctly under Apple's Safari. The DIV's do not align amd float as they should, and do in Dotnet. The page is really, really simple, and it has a CSS and with NO masterpage. I have tried using the following as recommendations made earlier in this Newsgroup:-
0
8466
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...
1
8591
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
8659
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7412
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
6212
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4208
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2799
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
2037
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1791
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.