By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,725 Members | 1,094 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,725 IT Pros & Developers. It's quick & easy.

GetCustomAttributes: MarshalAsAttribute

P: n/a
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.
--------------------------------------
Jul 21 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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/<cl**********@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.
--------------------------------------

---
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]
Jul 21 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.