Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8558681
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T15:52:02+00:00 2026-06-11T15:52:02+00:00

Similar: How I can find Data Annotation attributes and their parameters using reflection However,

  • 0

Similar: How I can find Data Annotation attributes and their parameters using reflection

However, when attempting to gather the custom attribute, I always get back the same result. An empty ScriptIgnore.

PropertyInfo[] Properties = Entity.GetType().GetProperties();
foreach (PropertyInfo Property in Properties)

Upon debug, this line of code

var annotes = Property.GetCustomAttributes(typeof(ScriptIgnoreAttribute), false);

(I also tried using true)

looks like this

annotes | {System.Web.Script.Serialization.ScriptIgnoreAttribute[0]}

However, Property is defined as a class property like this

public virtual Lot Lot { get; set; }

There is no [ScriptIgnore] attribute attached. Moreover, when I have tried this on Property when it was defined like this

[ScriptIgnore]
public virtual ICollection<Lot> Lots { get; set; }

I get back the same result as above

annotes | {System.Web.Script.Serialization.ScriptIgnoreAttribute[0]}

How can I use reflection to determine if an attribute exists? Or other means if possible, I also tried

var attri = Property.Attributes;

but it did not contain any attributes.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T15:52:04+00:00Added an answer on June 11, 2026 at 3:52 pm

    The following code works:

    using System.Web.Script.Serialization;
    public class TestAttribute
    {
      [ScriptIgnore]
      public string SomeProperty1 { get; set; }
    
      public string SomeProperty2 { get; set; }
    
      public string SomeProperty3 { get; set; }
    
      [ScriptIgnore]
      public string SomeProperty4 { get; set; }
    }
    

    Define a static extension:

    public static class AttributeExtension
    {
      public static bool HasAttribute(this PropertyInfo target, Type attribType)
      {
        var attribs = target.GetCustomAttributes(attribType, false);
        return attribs.Length > 0;
      }
    }
    

    Put the following sample code into a method and it picks up the attribute correctly – including ICollection by the way:

      var test = new TestAttribute();
      var props = (typeof (TestAttribute)).GetProperties();
      foreach (var p in props)
      {
        if (p.HasAttribute(typeof(ScriptIgnoreAttribute)))
        {
          Console.WriteLine("{0} : {1}", p.Name, attribs[0].ToString());
        }
      }
    
      Console.ReadLine();
    

    NOTE: if you’re using EF dynamic proxy classes, I think you will need to use ObjectContext.GetObjectType() to resolve to the original class before you can get the attributes, since the EF-generated proxy class will not inherit the attributes.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I use java reflection or similar to find project name by just
I know this question is similar to several previous ones, but I can't find
Maybe is a often repeated question here, but i can't find anything similar with
Had a good read through similar topics but I can't quite a) find one
Where can I find some good tutorials with examples with free data, on how
I know that I was already posted similar question but I just can't find
I access data in .dbf files via System.Data.OleDb (vfpoledb.dll). How can I find out
Very simple question (surprisingly I can't find a similar question anywhere): how do I
I am first time asking question here, sorry, but I can not find similar
[The only similar question I can find was answered long ago and contains a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.