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 / New Users / October 2004

Tip: Looking for answers? Try searching our database.

GetCustomAttributes: MarshalAsAttribute

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Chakravarthy Bollapalli \(IFIN SCC COM\) - 27 Oct 2004 07:52 GMT
Hi,
   I tried to retrieve CustomAttributes from a field in a class that has a
MarshalAs attribute but I couldn't.

Out of frustration I tried all possible combinations.

GetCustomAttributes(true);
GetCustomAttributes(false);
GetCustomAttributes(typeof(MarshalAsAttribute), true);
GetCustomAttributes(typeof(MarshalAsAttribute), false);

but all 4 calls consistently gave me 0 size sized array.

Any help or pointers on how to retrieve "MarshalAs" attributes ?

TIA
kalyan

=============================================================
PS:

Why do .GetType() calls on objects of classes having
"[StructLayout(LayoutKind.Sequential)]"
attribute throw the following exception.

----------------------------------
An unhandled exception of type 'System.TypeLoadException' occurred in
Test.exe

Additional information: Could not load type Test.ServerInfo from assembly
Test, Version=1.0.1761.19867, Culture=neutral, PublicKeyToken=null because
the format is invalid.
--------------------------------------
Richard Blewett [DevelopMentor] - 27 Oct 2004 08:51 GMT
Unfortunately you can't. MarshalAs (and the other interop attributes) are known as Pseudo Custom Attributes (PCAs) and are not actually held in .custom metadata tags in the IL which the reflection API looks for.

If you compile the folllowing:

using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential)]
public class Person
{
 [MarshalAs(UnmanagedType.LPWStr)]
 public string Name;
 [MarshalAs(UnmanagedType.VariantBool)]
 public bool Happy;
}

and look at the IL in ILDASM you will see, for example, the Name field declared as follows:

  .field public  marshal( lpwstr) string Name

The data is obviously there but not in the form you are looking for - I imagine you'd have to look via the unmanaged metadata API. IIRC, in Whidbey they are special casing some of the PCAs so they are retrievable via the GetCustomAttributes mechanism - whether MarshalAs is one of these though I'm not sure.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

  nntp://news.microsoft.com/microsoft.public.dotnet.framework/<clngk2$f55$1@athen03.muc.infineon.com>

Hi,
I tried to retrieve CustomAttributes from a field in a class that has a
MarshalAs attribute but I couldn't.

Out of frustration I tried all possible combinations.

GetCustomAttributes(true);
GetCustomAttributes(false);
GetCustomAttributes(typeof(MarshalAsAttribute), true);
GetCustomAttributes(typeof(MarshalAsAttribute), false);

but all 4 calls consistently gave me 0 size sized array.

Any help or pointers on how to retrieve "MarshalAs" attributes ?

TIA
kalyan

=============================================================
PS:

Why do .GetType() calls on objects of classes having
"[StructLayout(LayoutKind.Sequential)]"
attribute throw the following exception.


----------------------------------
An unhandled exception of type 'System.TypeLoadException' occurred in
Test.exe

Additional information: Could not load type Test.ServerInfo from assembly
Test, Version=1.0.1761.19867, Culture=neutral, PublicKeyToken=null because
the format is invalid.
--------------------------------------





[microsoft.public.dotnet.framework]

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.