Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / May 2008

Tip: Looking for answers? Try searching our database.

How to find argument type (in] [out] using reflection?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Fiz - 08 May 2008 04:00 GMT
I'm using reflection API's to find the argument types of a methods (such as
in or out etc). For example, consider this method

public int outerFoo(string str, ref double db) { return 42; }

When using reflection, I would like to know if str is [in] and db is
[in,out]. I got the code written like this

for each ( ParameterInfo^ pi in (((MethodInfo^) methInfo)->GetParameters()))
{
if(pi->IsIn)
..do something
else if(pi->IsOut)
..do something else
}

But IsIn and IsOut always return false. Are these deprecated? Any workarounds?

Any help would be appreciated
Thanks
Fiz
Ben Voigt [C++ MVP] - 08 May 2008 15:10 GMT
> I'm using reflection API's to find the argument types of a methods
> (such as in or out etc). For example, consider this method
[quoted text clipped - 14 lines]
> But IsIn and IsOut always return false. Are these deprecated? Any
> workarounds?

Did you call GetCustomAttributes and look for InAttribute and OutAttribute?

> Any help would be appreciated
> Thanks
> Fiz
Fiz - 09 May 2008 08:40 GMT
Thanks for the response.

I tried attributes, it did not return anything.

array<Object^>^myAttributes = pi->GetCustomAttributes(false);

Length of myAttributes was 0. I also tried passing specific InAttribute and
OutAttribute typeid, that did not return either.

for each(Attribute^ attr in pi->GetCustomAttributes(InAttribute::typeid,
false))
{
do something..
}
               
I tried ParameterInfo->Attributes property, that returned empty

if(pi->Attributes == ParameterAttributes::In)
{
...
}

What else can I do here?

     

> > I'm using reflection API's to find the argument types of a methods
> > (such as in or out etc). For example, consider this method
[quoted text clipped - 20 lines]
> > Thanks
> > Fiz
Ben Voigt [C++ MVP] - 09 May 2008 19:16 GMT
> Thanks for the response.
>
[quoted text clipped - 19 lines]
>
> What else can I do here?

Note also that the ParameterType->IsByRef is closely related to out and ref
parameters.  In fact, if that isn't true, I think you can safely assume the
parameter is "In".  For parameters which are ByRef, you need to check for
ref vs out.

>>> I'm using reflection API's to find the argument types of a methods
>>> (such as in or out etc). For example, consider this method
[quoted text clipped - 21 lines]
>>> Thanks
>>> Fiz
Fiz - 09 May 2008 21:11 GMT
> Note also that the ParameterType->IsByRef is closely related to out and ref
> parameters.  In fact, if that isn't true, I think you can safely assume the
> parameter is "In".  For parameters which are ByRef, you need to check for
> ref vs out.

Thanks. However, I'm back to the same question. If it is ByRef, how do I
check for ref vs out. I checked the properties and methods of ParameterType ,
couldn't find anything. Any hint? Any URL?

It shouldn't be that difficult, I'm missing something here :(
Ben Voigt [C++ MVP] - 09 May 2008 22:50 GMT
>> Note also that the ParameterType->IsByRef is closely related to out
>> and ref parameters.  In fact, if that isn't true, I think you can
[quoted text clipped - 6 lines]
>
> It shouldn't be that difficult, I'm missing something here :(

ParameterInfo.Attributes and ParameterInfo.IsOut are working for me.

code:
   [TestFixture]
   public class OutParameter
   {
       [Test]
       public static void test()
       {
           foreach (MethodInfo mi in
typeof(double).GetMethods(BindingFlags.Public | BindingFlags.Instance |
BindingFlags.Static))
           {
               System.Diagnostics.Trace.WriteLine(mi.Name + " : ");
               foreach (ParameterInfo pi in mi.GetParameters())
               {
                   System.Diagnostics.Trace.WriteLine(pi.Name + " > " +
pi.Attributes);
               }
           }
       }
   }

output:
[snip]
TryParse :
s > None
result > Out
[snip]

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.