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

create nullable type with string

i need to get an object of type Type when i have a string. That is the
easy part (Type.GetType("int")). What if i want that type to be
nullable? This doesn't work Type.GetType("int?"). It returns null.

This does work but i don't know how to know all that information at runtime.

Type.GetType("System.Nullable`1[[System.Int32, mscorlib,
Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]")

dan
Oct 23 '07 #1
1 2481
Dan,

Assuming you have the type for T in Nullable<T>, you can do the
following:

// This is where you would get typeof(int)
Type typeParameter = ...

// Get the Nullable<Tgeneric type.
Type nullable = typeof(Nullable<>);

// Now make the constructed type.
Type constructedType = nullable.MakeGenericType(new Type[]{typeParameter});

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Dan Holmes" <da*******@bigfoot.comwrote in message
news:uH**************@TK2MSFTNGP04.phx.gbl...
>i need to get an object of type Type when i have a string. That is the
easy part (Type.GetType("int")). What if i want that type to be nullable?
This doesn't work Type.GetType("int?"). It returns null.

This does work but i don't know how to know all that information at
runtime.

Type.GetType("System.Nullable`1[[System.Int32, mscorlib, Version=2.0.0.0,
Culture=neutral, PublicKeyToken=b77a5c561934e089]]")

dan

Oct 23 '07 #2

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

Similar topics

3
by: Sakoulakis | last post by:
Hi, I was just reading about nullable types and I like that feature very much. What dissapointed me is that nullable string is not supported. Imagine a field (of type sting) in a database...
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...
27
by: Yuriy Solodkyy | last post by:
Hi VS 2005 beta 2 successfully compiles the following: using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
15
by: WHY | last post by:
Since there's no way to create a c# method with optional, or nullable parameters. And since you can't write an overloaded web method. Is it possible to edit the WSDL in conjunction with a c#...
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...
8
by: shawnk | last post by:
Given several nullable boolean flags; bool? l_flg_01 = true; bool? l_flg_02 = false; bool? l_flg_03 = true; bool? l_result_flg = null; I would have liked...
1
by: Joe Bloggs | last post by:
Hi, Can someone please kindly show me how to determine if a type (read value type) is Nullable. MSDN has this KB: How to: Identify a Nullable Type (C# Programming Guide)...
8
by: Sam Kong | last post by:
Hello, I want to define a generic class which should accept only nullable types or reference types. What's the best way to costrain it? --------- class MyClass<T>{ ...
7
by: tshad | last post by:
Can you handle nullable string vb.net 2.0? You can with Integer but I get an error when I try to do a Dim s as Nullable (Of String) Now strings are a nullable type but it can't handle: ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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?
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
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
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.