473,320 Members | 1,933 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

GetCustomAttributes: MarshalAsAttribute

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
1 2386
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: William Stacey | last post by:
Using the following struct def, how can I tell (using reflection) if "ba" has the marshal attribute and get the "ByValArray" and maybe even the size? In the bigger picture, given a struct (or a...
2
by: CA | last post by:
I am learning about attributes and at this point, I am completely confused. I have posted some code below. I do not understand the following points: 1) The TestAttribute does not appear for...
0
by: rein.petersen | last post by:
Hi All, I've been struggling for a way to retrieve custom attributes I have set (SetCustomAttributes) on a class through a TypeBuilder but can't seem to get around : "NotSupportedException -...
0
by: Edson Valentim via .NET 247 | last post by:
Hey, (my english is bad) I'm Sofware Developer in Brazil. I'm have a big trouble. Description: A application in VB6 use a struct file in C. Struct Components Public Structure str_parent
1
by: Chakravarthy Bollapalli \(IFIN SCC COM\) | last post by:
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. ...
0
by: Elad | last post by:
Hi to you all, I'm using the following code (Code Sample 1) to determine whether an assembly is a BizTalk assembly or not. When I run the code in different machines I fount out that it does not...
1
by: Alex | last post by:
I have a simple enough struct.. public struct InstallationGeneral_Lock { public int Case_name; public int Run_year; public int Inst_name; public int Inst_loc; public int State; public int...
0
by: Marc Vangrieken | last post by:
Hi, I ran into a strange problem a few days ago; when i execute the code at the bottom of this message GetCustomAttributes() isn't returning anything. Although the attributes are there... The...
1
by: not_a_commie | last post by:
I've got some custom serialization code that relies heavily on Type.GetCustomAttributes, FieldInfo.GetCustomAttributes, and PropertyInfo.GetCustomAttributes. They are all incredibly slow. Those...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.