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(La youtKind.Sequen tial)]
public class Person
{
[MarshalAs(Unman agedType.LPWStr )]
public string Name;
[MarshalAs(Unman agedType.Varian tBool)]
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 GetCustomAttrib utes 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.publi c.dotnet.framew ork/<cl**********@a then03.muc.infi neon.com>
Hi,
I tried to retrieve CustomAttribute s from a field in a class that has a
MarshalAs attribute but I couldn't.
Out of frustration I tried all possible combinations.
GetCustomAttrib utes(true);
GetCustomAttrib utes(false);
GetCustomAttrib utes(typeof(Mar shalAsAttribute ), true);
GetCustomAttrib utes(typeof(Mar shalAsAttribute ), 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(La youtKind.Sequen tial)]"
attribute throw the following exception.
----------------------------------
An unhandled exception of type 'System.TypeLoa dException' occurred in
Test.exe
Additional information: Could not load type Test.ServerInfo from assembly
Test, Version=1.0.176 1.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.publi c.dotnet.framew ork]