Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / CLR / July 2004

Tip: Looking for answers? Try searching our database.

How do I locate custom attributes on private fields?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ken Varn - 22 Jul 2004 16:53 GMT
I have a custom attribute that I would like to locate on fields within a
class object.  I can only find the attribute if it is placed on a public
field.  Private fields cannot be found.  I have tried the following code,
but not sure why it is not finding the attribute when the field is marked
private.

int CountAttribute(object StateObj)
{
    Type T = StateObj.GetType();
    int Count = 0;
    MemberInfo[] MemberArray;
    object[] AttributeArray;

        MemberArray = T.FindMembers(MemberTypes.Property |
MemberTypes.Field,
                                    BindingFlags.DeclaredOnly |
BindingFlags.Instance |
                                    BindingFlags.Public |
BindingFlags.NonPublic | BindingFlags.Static,
                                    Type.FilterName,"*");

        foreach (MemberInfo M in MemberArray)
        {
           AttributeArray =
M.GetCustomAttributes(typeof(MyCustomAttribute),true);

           foreach(MyCustomAttribute A in AttributeArray)
           {
              ++Count;
           }
        }

     return Count;
 }

Signature

-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------

Eric Cadwell - 23 Jul 2004 16:50 GMT
This is working fine for me.

Can you post more info - sample class and result? I created a class with 1
private, 1 public and 1 static field, then marked them all with
MyCustomAttribute. I ran the app and it said there were 3 fields as
expected.

HTH;
Eric Cadwell
http://www.origincontrols.com
Ken Varn - 26 Jul 2004 19:54 GMT
My app is ASP.NET.  Maybe it is a security issue?  Here are some code
snippets:

public class WebForm1 : System.Web.UI.Page
{
     [MaintainState(StateType.View)]         // This user-defined attribute
does not show up in list.
     private String MyValue;                      // If I change this to
public, it works fine.
....

} // end of class

-----------------------------------------------------
The attribute class is defined as follows:

[AttributeUsage(AttributeTargets.Field | AttributeTargets.Property)]
public class MaintainStateAttribute : Attribute
{
   private StateType _SType;

 public MaintainStateAttribute(StateType S)        // StateType is an
enumerator.
 {
    _SType = S;
 }

// This static method in the attribute class tries to get the attributes for
the passed in object.
// The object passed in is of the class defined WebForm1 (snippet shown
above).
public static void GetAttrs(object StateObj)
{
        Type T = StateObj.GetType();
        int Count = 0;
        MemberInfo[] MemberArray;
        object[] AttributeArray;

        MemberArray = T.GetMembers(BindingFlags.Instance |
BindingFlags.Static |
                                      BindingFlags.Public |
BindingFlags.NonPublic);

        foreach (MemberInfo M in MemberArray)
        {
           AttributeArray =
M.GetCustomAttributes(typeof(MaintainStateAttribute),true);

           foreach(MaintainStateAttribute A in AttributeArray)
           {
              ++Count;
           }
        }

// Count is 0 when I get here.
}
}

public enum StateType
{
   Application,
   Session,
   View,
   Cookie
}

Signature

-----------------------------------
Ken Varn
Senior Software Engineer
Diebold Inc.

EmailID = varnk
Domain = Diebold.com
-----------------------------------

> This is working fine for me.
>
[quoted text clipped - 6 lines]
> Eric Cadwell
> http://www.origincontrols.com

Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.