473,395 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Determing if a Type is a specific Generic

Hi,
I'm in the process of upgrading an Object-Relational mapping library to .NET
2.0 and am wanting to implement support for Nullable typess.

I'm using Reflection to analyise the object that is being mapped and
building up a schema for the mapping.

This means I need a way to determine if the PropertyInfo.PropertyType is an
implementation of System.Nullable<>. I've been batting my head against this
for a while and only come up with one solution.

private static bool IsNullable(Type type)
{
if(!type.IsGenericType)
return false;

Type[] parameters = new Type[] { type.GetGenericArguments()[0] };

Type constructedType = typeof(Nullable<>).MakeGenericType(parameters);

return type == constructedType;
}

which I think is less than optimal.

Any suggestions?
Mar 26 '06 #1
2 1882
"Nigel Sampson" <Nigel Sa*****@discussions.microsoft.com> a écrit dans le
message de news: B8**********************************@microsoft.com...

| I'm in the process of upgrading an Object-Relational mapping library to
..NET
| 2.0 and am wanting to implement support for Nullable typess.
|
| I'm using Reflection to analyise the object that is being mapped and
| building up a schema for the mapping.
|
| This means I need a way to determine if the PropertyInfo.Property Type is
an
| implementation of System.Nullable<>. I've been batting my head against
this
| for a while and only come up with one solution.
|
| private static bool IsNullable(Type type)
| {
| if(!type.IsGenericType)
| return false;
|
| Type[] parameters = new Type[] { type.GetGenericArguments()[0] };
|
| Type constructedType = typeof(Nullable<>).MakeGenericType(parameters);
|
| return type == constructedType;
| }

Try this :

return type.IsGenericType && type.GetGenericTypeDefinition() ==
typeof(Nullable<>);

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer
Mar 26 '06 #2
Cheers, will try that tonight, I suspect my main issue when trying to
determine a decent method was not knowing that typeof(Nullable) and
typeof(Nullable<>) where two different types.

"Joanna Carter [TeamB]" wrote:
"Nigel Sampson" <Nigel Sa*****@discussions.microsoft.com> a écrit dans le
message de news: B8**********************************@microsoft.com...

| I'm in the process of upgrading an Object-Relational mapping library to
..NET
| 2.0 and am wanting to implement support for Nullable typess.
|
| I'm using Reflection to analyise the object that is being mapped and
| building up a schema for the mapping.
|
| This means I need a way to determine if the PropertyInfo.Property Type is
an
| implementation of System.Nullable<>. I've been batting my head against
this
| for a while and only come up with one solution.
|
| private static bool IsNullable(Type type)
| {
| if(!type.IsGenericType)
| return false;
|
| Type[] parameters = new Type[] { type.GetGenericArguments()[0] };
|
| Type constructedType = typeof(Nullable<>).MakeGenericType(parameters);
|
| return type == constructedType;
| }

Try this :

return type.IsGenericType && type.GetGenericTypeDefinition() ==
typeof(Nullable<>);

Joanna

--
Joanna Carter [TeamB]
Consultant Software Engineer

Mar 26 '06 #3

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

Similar topics

3
by: Simon G Best | last post by:
Hello! The C++ standard library provides facilities for finding out the sizes (and other such stuff) of numeric types (::std::numeric_limits<>, for example). What I would like to do is to...
29
by: shmartonak | last post by:
For maximum portability what should the type of an array index be? Can any integer type be used safely? Or should I only use an unsigned type? Or what? If I'm using pointers to access array...
8
by: Leszek Taratuta | last post by:
Hello, I have the following code: // Load the MenuBar.ascx user control. The control defines SavePropertyEvent event. UserControl ctrl =...
4
by: Charles Churchill | last post by:
I apologize if this question has been asked before, but after about half an hour of searching I haven't been able to find an answer online. My code is beloiw, with comments pertaining to my...
59
by: peter.tornqvist | last post by:
Maybe I'm stupid or maybe I am missing the obvious, but I can't find a way to define a type alias in C# for primitive types (class and interfaces I can inherit from, no problem). I.e I want to...
9
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
9
by: Steve Richter | last post by:
in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want to write, I have a call to...
6
by: Andrus | last post by:
I need to create method which returns my application object type as string : TypeName(typeof( List<int>)) should return "List<int>" or "System.Collections.Generic.List<int>" ...
2
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have 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
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,...
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...

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.