The Artima Developer Community
Sponsored Link

.NET Buzz Forum
Getting the true type of a parameter passed by ref or out via reflection

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Jeff Key

Posts: 481
Nickname: jeffreykey
Registered: Nov, 2003

Jeff Key is legally sane, but questionably competent.
Getting the true type of a parameter passed by ref or out via reflection Posted: May 23, 2004 12:27 AM
Reply to this message Reply

This post originated from an RSS feed registered with .NET Buzz by Jeff Key.
Original Post: Getting the true type of a parameter passed by ref or out via reflection
Feed Title: Jeff Key
Feed URL: http://www.asp.net/err404.htm?aspxerrorpath=/jkey/Rss.aspx
Feed Description: Topics revolve around .NET and the Windows platform.
Latest .NET Buzz Posts
Latest .NET Buzz Posts by Jeff Key
Latest Posts From Jeff Key

Advertisement

Getting the type of a parameter passed by value via reflection is simple; however, if the parameter is a ref or out, the story's a bit different. For example, a ref or out DateTime parameter's type is actually DateTime&, which is not the same as DateTime and type equality comparisons will fail. Luckily for us, the solution is easy.

Instead of calling ParameterInfo.ParameterType, you need to call ParameterInfo.GetElementType(). Type also has a HasElementType property that indicates wheether you need to call GetElementType().

public static Type GetParameterType(ParameterInfo paramInfo)
{
    if (paramInfo.ParameterType.HasElementType)
    {
        return paramInfo.ParameterType.GetElementType();
    }
    else
    {
        return paramInfo.ParameterType;
    }
}

Read: Getting the true type of a parameter passed by ref or out via reflection

Topic: Our own Casey Chesnut contributes Sys.Sec.Crypto namespace to OpenNETCF Previous Topic   Next Topic Topic: Are Flashy WinForms Threats to Their Own Species? Let's get .NET Flashy!

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use