473,397 Members | 1,985 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,397 software developers and data experts.

Nullable parameter type name

My type contains constructor with nullable parameter like

public class Iandmed {
public Iandmed(DateTime? miskuup) {}
}

I tried to get its constructor parameter type name using

Type t = typeof(Iandmed);
ConstructorInfo c = t.GetConstructors()[0];
ParameterInfo p = c.GetParameters()[0];

p.ParameterType.Name returns wrong values for nullable types, in this case

"Nullable`1"

How to get exact type name:

"DateTime?"

Andrus.
Nov 11 '07 #1
2 7911
Andrus <ko********@hot.eewrote:
My type contains constructor with nullable parameter like

public class Iandmed {
public Iandmed(DateTime? miskuup) {}
}

I tried to get its constructor parameter type name using

Type t = typeof(Iandmed);
ConstructorInfo c = t.GetConstructors()[0];
ParameterInfo p = c.GetParameters()[0];

p.ParameterType.Name returns wrong values for nullable types, in this case

"Nullable`1"

How to get exact type name:

"DateTime?"
Don't use the Name property - use other properties to get specific
parts. In particular, the FullName property gives
System.Nullable`1[[System.DateTime, mscorlib, Version=2.0.0.0,
Culture=neutral,PublicKeyToken=b77a5c561934e089]]

Alternatively you can use
p.ParameterType.GetGenericArguments()[0]

to get the DateTime part on its own, or
Nullable.GetUnderlyingType(p.ParameterType)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 11 '07 #2
On 2007-11-11 15:28:06 -0800, "Andrus" <ko********@hot.eesaid:
[...]
p.ParameterType.Name returns wrong values for nullable types, in this case

"Nullable`1"

How to get exact type name:

"DateTime?"
I don't have the answer off the top of my head. But I can tell you
that you are getting the correct type name. "DateTime?" is actually
equivalent to "Nullable<DateTime>", thus the generated generic type
name.

You'll want to research how to obtain the generic paramter type
information from a concrete instance of a generic class. That will
provide the information you're looking for. Unfortunately, I don't use
reflection enough to be able to tell you the exact answer, but
hopefully that gets you pointed in the right direction.

Pete

Nov 11 '07 #3

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

Similar topics

4
by: ESPNSTI | last post by:
Hi, Please don't shoot me if the answer is simple, I'm new to .Net and C# :) .. I'm attempting to convert a nullable type to it's "non-nullable" type in a generic way (without knowing what...
5
by: Kevin Yu | last post by:
hi all since the DateTime can't be assign null, the min value is set to 1901 or something, if in a application design, the date field can be null, so in between the UI and the DB, the business...
4
by: Mark Rae | last post by:
Hi, In v1.1 I used to simulate nullable datatypes with structs. e.g. public struct NullableString { private readonly String pstrValue; private readonly bool blnHasValue; public...
6
by: Steven Livingstone | last post by:
Bit of advice here folks. I am creating a default constructor that just initializes a new instance of my object and a secon constructor that takes an ID and loads an object with data from the...
2
by: Joe Bloggs | last post by:
Hi, compiling the following code: public class App { static void Main() { int? x = 5; bool? i = null;
5
by: Niall | last post by:
I'm experience problems very similar to those reported in this thread on dotnet.framework on May 16 at...
3
by: senfo | last post by:
I'm trying to pass a Nullable type as a parameter to the TryParse() method (both int and float types) and I'm not having any luck. I've tried casting as well as passing just the T.Value property;...
6
by: =?Utf-8?B?VGVycnk=?= | last post by:
I have a generic function I am using to check constraints, an example of which is the following: Public Function ValidateMinValue(Of t As IComparable)(ByVal PropertyName As String, ByVal value...
2
by: jon.rea | last post by:
Please tell me there is a better way :-D .... public static bool IsNullableType(Type paramType) { return paramType.IsGenericType && paramType.GetGenericTypeDefinition() == typeof...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
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...
0
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
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...
0
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,...

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.