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

  • Home
  • SEARCH
  • 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 790863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:44:39+00:00 2026-05-14T21:44:39+00:00

I have an attribute lets call it SomeAttribute and a class i need to

  • 0

I have an attribute lets call it SomeAttribute and a class i need to make sure the class is passed a type which has SomeAttribute. So this is how i do it now:

public class Test()
{
    public Test(SomeType obj)
    {
        if(!obj.GetType().IsDefined(typeof(SomeAttribute), false))
        {
            throw new ArgumentException("Errormessage");
        }
    }
}

But this means that i don’t get any errors at compile time but somewhere at runtime, if obj does not have the attribute. Is there a way to specify in the method declaration that the parameter must have some attribute ? So i get errors i compile time when using the wrong parameters, or do i have to use an empty interface ?

  • 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-05-14T21:44:39+00:00Added an answer on May 14, 2026 at 9:44 pm

    No, there’s no way of requiring an attribute – but you could always write unit tests to scan through your assemblies and check that the appropriate attributes had been applied – assuming you can actually detect this requirement.

    It sounds somewhat odd though – could you give some more details as to why you’re using this scheme? Perhaps another approach would be more appropriate, such as another parameter to describe the extra data you’re interested in.

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

Sidebar

Related Questions

I have a custom attribute which can be assigned to a class, [FooAttribute] .
I have a problem applying the DebuggerDisplay attribute on a generic class: [DebuggerDisplay(--foo--)] class
I have a list of dynamically generated div-tags with a class-attribute but no id.
Let's say I have many-to-many relationship (using the ActiveRecord attribute HasAndBelongsToMany) between Posts and
I know I could have an attribute but that's more work than I want
In my user model, I have an attribute called nickname and validates as such:
I have a filter attribute that I'd like to be applied to every action
According to HTML specs, the select tag in HTML doesn't have a readonly attribute,
I have a frameset with it's cols attribute set to 50%,50% at the moment.
I have the following code: for attribute in site.device_attributes device.attribute end where I would

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.