473,804 Members | 3,958 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

GetCustomAttrib utes: MarshalAsAttrib ute

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

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
3828
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 class), how to decide if "sizeof(MyStruct)" would fail before calling sizeof and having to use Marshal.sizeof? TIA public struct MyStruct {
2
2419
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 the StructureChanged field, i.e. the GetCustomAttributes() method does not return any attributes event though the field has been flagged.
0
1326
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 - This method is not currently supported for incomplete types. Retrieve the type using GetType and call GetCustomAttributes on the returned Type."
0
1471
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
384
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. GetCustomAttributes(true); GetCustomAttributes(false); GetCustomAttributes(typeof(MarshalAsAttribute), true); GetCustomAttributes(typeof(MarshalAsAttribute), false);
0
3188
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 always has the same results. I also tried the second code sample (Code Sample 2)but still, I only got the attributes that are not BizTalkAssembly attributes. My guess is that this has to do with permissions, but I'm not quite sure in what way.
1
2219
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 Weather_filename;
0
1408
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 code is correct, i think. Does anyone know what might be causing the problem? foreach (MethodInfo method in this.GetType().GetMethods()) { methodSequenceNumber = -1;
1
8468
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 three account for 90% of the time in my code. Would it be wise to cache them in a dictionary? I doubt FieldInfo and PropertyInfo have well-implemented hash codes. And is the Property/ FieldInfo returned not dependent upon the owning instance? ...
0
10338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7622
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5658
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3823
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2997
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.