473,795 Members | 3,215 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Confused about GetCustomAttrib utes from FieldInfo

CA
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 StructureChange d field,
i.e. the GetCustomAttrib utes() method does not return any attributes
event though the field has been flagged.

2) If I remove the Bindings.NonPub lic flag, the field StructureChange d
does not appear in the fields array.

I appreciate any help in clarifying my thoughts.

Regards

Chris
--------------------------------------------------
public void TestCode()
{
TesterObjectCla ss toc = new TesterObjectCla ss();
Type t = toc.GetType();
FieldInfo[] fields =
t.GetFields(Bin dingFlags.NonPu blic|BindingFla gs.Public|Bindi ngFlags.Instanc e|BindingFlags. DeclaredOnly);
Type TestAttr = typeof(TestAttr ibute);
foreach (FieldInfo fi in fields)
{
object[] attr = fi.GetCustomAtt ributes(TestAtt r, false);
// StructureChange d : attr.Length = 0; vIsBoolean : attr.Length = 1;
Console.WriteLi ne("Field Name: {0}, Length: {1}", fi.Name,
attr.Length);

}
}

[AttributeUsage( System.Attribut eTargets.All,
AllowMultiple=f alse,
Inherited=false )]
public class TestAttribute : Attribute
{
}

public class TesterObjectCla ss
{

[TestAttribute] public event EventHandler StructureChange d;
[TestAttribute] public bool vIsBoolean;
public double vIsDouble;
}
Nov 15 '05 #1
2 2417
Chris,
I am learning about attributes and at this point, I am completely
confused.
First you have to understand that the syntax

public event EventHandler StructureChange d;

is sort of a shortcut for the more explicit version

private EventHandler StructureChange d;
public event EventHandler StructureChange d
{
add { ... }
remove { ... }
}

So it compiles down to a private delegate field and add and remove
accessor methods for the event.

1) The TestAttribute does not appear for the StructureChange d field,
i.e. the GetCustomAttrib utes() method does not return any attributes
event though the field has been flagged.
The attribute is associated with the event by default. To put it on
the underlying field, you use

[field: TestAttribute] public event EventHandler StructureChange d;

2) If I remove the Bindings.NonPub lic flag, the field StructureChange d
does not appear in the fields array.


Because the delegate field is private, only the event is public.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #2
CA
Mattias,

That was a very elegant explanation. Thank you for clearing up my confusion.

Regards

Chris

Mattias Sjögren <ma************ ********@mvps.o rg> wrote in message news:<ej******* *******@TK2MSFT NGP12.phx.gbl>. ..
Chris,
I am learning about attributes and at this point, I am completely
confused.


First you have to understand that the syntax

public event EventHandler StructureChange d;

is sort of a shortcut for the more explicit version

private EventHandler StructureChange d;
public event EventHandler StructureChange d
{
add { ... }
remove { ... }
}

So it compiles down to a private delegate field and add and remove
accessor methods for the event.

1) The TestAttribute does not appear for the StructureChange d field,
i.e. the GetCustomAttrib utes() method does not return any attributes
event though the field has been flagged.


The attribute is associated with the event by default. To put it on
the underlying field, you use

[field: TestAttribute] public event EventHandler StructureChange d;

2) If I remove the Bindings.NonPub lic flag, the field StructureChange d
does not appear in the fields array.


Because the delegate field is private, only the event is public.

Mattias

Nov 15 '05 #3

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

Similar topics

1
2445
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);
2
1423
by: dotnetguy | last post by:
Hello, having a FieldInfo object I'd like to get a reference to the corresponding field object. I know the type of the field object (so I can do a cast if it is required) but I don't know if FieldInfo indeed provides a reference to the underlying object and how to get it. Can anyone help out??? Thx.
0
2165
by: James Hadwen | last post by:
Hi, I'm trying to do something with reflection that's a little wierd. The idea is that I call for a field to be updated on a base class. Using reflection it first updates the value, and then updates the change that has been made to a dictionary. The code below is just to encourage some ideas, if you look at most of the code you find that most of it is pretty pointless. I might as well have Translate(string fieldname, ref string...
2
2987
by: Robert W. | last post by:
I'm trying to write a utility that will use Reflection to examine any data model I pass it and correctly map out this model into a tree structure. When I say "any" , in fact there will only be 3 types of items in the very hierarchical data model: - Classes (and nested classes) - Collections - Properties I've successfully written the Reflection code to handle any combination of classes and properties but I'm confused about what to do...
3
3117
by: Laurie | last post by:
Hi, I need to be able to manipulate field values within a structure using FieldInfo.GetValue and FieldInfo.SetValue, in VB.Nt 2003. The GetValue is working fine and makes it really easy for me to maintain my code. Unfortunately, SetValue doesn't return any errors but doesn't actually set the value. I have read through various newsgroup postings, and think the problem might have something to do with "boxing", and passing of structures...
5
3963
by: Dennis | last post by:
I have an array "t" where each element is a structure of type "testing". I want to use the FieldInfo class to set the values of the array element fields. I can read the field properties and return the velue using this technique but cannot set the field value as per below. Why won't it change the value. If define "testing" as a class where col1 and col2 are fields as opposed to properties, it works fine. Just won't work when "testing"...
1
2205
by: David Gouge | last post by:
Posting on behalf of a colleague (no, really! ;) )... Hi, I was wondering if there is any equivalent of FieldInfo.SetValue(Object, Object) that is strongly typed or uses generics? I have been looking at the source for FieldInfo to see if I could implement my own generic version of this method but unfortunately the code seems to be hidden, is there anyway I can implement my own method if there is no equivalent?
7
8745
by: Wilson | last post by:
Hi, How do get the Dictioanry object from FiedlInfo ? my code : fieldInfo = this.GetType().GetField("dictioanry1"); ??Dictionary<string, string> dicTemp1 = (Dictionary<string, string>)fieldInfo; Thanks Wilson
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
9672
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
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...
1
10165
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
10001
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
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

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.