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.
2) If I remove the Bindings.NonPublic flag, the field StructureChanged
does not appear in the fields array.
I appreciate any help in clarifying my thoughts.
Regards
Chris
--------------------------------------------------
public void TestCode()
{
TesterObjectClass toc = new TesterObjectClass();
Type t = toc.GetType();
FieldInfo[] fields =
t.GetFields(BindingFlags.NonPublic|BindingFlags.Pu blic|BindingFlags.Instance|BindingFlags.DeclaredOn ly);
Type TestAttr = typeof(TestAttribute);
foreach (FieldInfo fi in fields)
{
object[] attr = fi.GetCustomAttributes(TestAttr, false);
// StructureChanged : attr.Length = 0; vIsBoolean : attr.Length = 1;
Console.WriteLine("Field Name: {0}, Length: {1}", fi.Name,
attr.Length);
}
}
[AttributeUsage(System.AttributeTargets.All,
AllowMultiple=false,
Inherited=false)]
public class TestAttribute : Attribute
{
}
public class TesterObjectClass
{
[TestAttribute] public event EventHandler StructureChanged;
[TestAttribute] public bool vIsBoolean;
public double vIsDouble;
} 2 2386
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 StructureChanged;
is sort of a shortcut for the more explicit version
private EventHandler StructureChanged;
public event EventHandler StructureChanged
{
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 StructureChanged field, i.e. the GetCustomAttributes() 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 StructureChanged;
2) If I remove the Bindings.NonPublic flag, the field StructureChanged 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.
Mattias,
That was a very elegant explanation. Thank you for clearing up my confusion.
Regards
Chris
Mattias Sjögren <ma********************@mvps.org> wrote in message news:<ej**************@TK2MSFTNGP12.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 StructureChanged;
is sort of a shortcut for the more explicit version
private EventHandler StructureChanged; public event EventHandler StructureChanged { 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 StructureChanged field, i.e. the GetCustomAttributes() 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 StructureChanged;
2) If I remove the Bindings.NonPublic flag, the field StructureChanged does not appear in the fields array.
Because the delegate field is private, only the event is public. Mattias This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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.
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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,...
|
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...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |