GetCustomAttributes: MarshalAsAttribute

  • Thread starter Thread starter Chakravarthy Bollapalli \(IFIN SCC COM\)
  • Start date Start date
C

Chakravarthy Bollapalli \(IFIN SCC 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.
--------------------------------------
 
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/<[email protected]>

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.
--------------------------------------



---
Incoming mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.782 / Virus Database: 528 - Release Date: 22/10/2004



[microsoft.public.dotnet.framework]
 
Back
Top