473,795 Members | 3,081 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 2445
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
3827
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
2417
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
8467
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
9519
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
10439
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
9043
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
7541
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
6783
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
5437
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
5563
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4113
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
3
2920
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.