How to create a Nullable from a Type variable?

I'm working with expressions and I need a method which receives an object of some type (currently unknown). Something like this:

public static void Foobar(object Meh) { }

What I need to is make this method return a Nullable<T> version of Meh, but the type T is from Meh.GetType(). So the return would be Nullable<MehType>, where MehType is the type of Meh.

Any ideas or suggestions?


<strong>Update:</strong> the reason why I needed this is because of this exception:

The binary operator Equal is not defined for the types 'System.Nullable`1[System.Int32]' and 'System.Int32'.

return Expression.Equal(leftExpr, rightExpr);

where leftExpr is a System.Nullable1[[System.Int32 and rightExpr is a System.Int32.


If you don't know the type at compile time, the only way of expressing it is as object - and as soon as you box a nullable value type, you end up with either a null reference, or a boxed non-nullable value type.

So these snippets are exactly equivalent in terms of the results:

int? nullable = 3; object result = nullable; int nonNullable = 3; object result = nonNullable;

In other words, I don't think you can really express what you're trying to do.


Do you have to use Meh.GetType() instead of a generic? What about this?

public static Nullable<T> Foobar<T>(T Meh) where T : struct { }

I'm making the assumption that "some type" does not mean "any type", because the solution above would only work with value types.


  • Browse Files Server-side in ASP.NET
  • How do i deploy MVC3 website to server via ftp
  • Can't populate big chunk of data to mongodb using Node.js
  • F# comparing discriminated unions' by case identifier
  • Why String.ToString()? [duplicate]
  • PHP Order array based on elements dependency
  • Task.IsCancelled doesn't work
  • How to write string.Contains(someText) in expression Tree
  • How to get the index of element in the List in c#
  • Rails AREL .where statement
  • Javascript Array, Object, Date not defined
  • Losing my session variables
  • Ruby 1.8.6 Array#uniq not removing duplicate hashes
  • Get a trait object reference from a vector
  • Xmonad multiple submap key combos
  • php script is parsing content from RTE (tt_news) but internal links are not appearing as speaking ur
  • How can I mock ui-router's resolve values when testing a state's configuration?
  • cordova is not defined - cordova.js has already been loaded :: Ionic
  • Sending keystrokes/mouse clicks to a Java program with Autohotkey
  • Subclassing QGraphicsItem prevents me from being able to use itemAt() on a QGraphicsScene/View
  • What and where is mdimport
  • Does it make sense to call System.gc() and Thread.sleep() when working on Bitmaps?
  • Why Encoding.ASCII != ASCIIEncoding.Default in C#?
  • How to match http request and response using Jersey ContainerRequestFilter and ContainerResponseFilt
  • one Local Olampyad Questions on Informatic in 2011
  • Different response to non-authenticated users and AJAX calls
  • Get object from AWS S3 as a stream
  • Knitr HTML Loop - Some HTML output, some R output
  • How to model a transition system with SPIN
  • Python: how to group similar lists together in a list of lists?
  • How to include full .NET prerequisite for Wix Burn installer
  • KeystoneJS: Relationships in Admin UI not updating
  • How to get Windows thread pool to call class member function?
  • Django query for large number of relationships
  • costura.fody for a dll that references another dll
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize