473,804 Members | 4,153 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
9707
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9585
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10586
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
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...
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...
0
6856
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5525
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5658
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.