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 134183
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:34:03+00:00 2026-05-11T06:34:03+00:00

I’m writing a command-line interface to my project. The user enters create project foo,

  • 0

I’m writing a command-line interface to my project. The user enters ‘create project foo’, and it finds the controller responsible for ‘project’ and then invokes the Create method, passing ‘foo’ as the first argument.

It relies heavily on attributes and reflection: the controller looks something like this:

[ControllerFor('project')] class ProjectController {     [ControllerAction('create')]     public object Create(string projectName) { /* ... */ } } 

I’d like to use Moq in the unit tests for the parser, something like this:

Mock<IProjectsController> controller = new Mock<IProjectsController>(); controller.Expect(f => f.Create('foo'));  parser.Register(controller.Object); parser.Execute('create project foo');  controller.VerifyAll(); 

Adding the attributes to the interface doesn’t appear to work — they’re not inherited by derived classes.

Can I get Moq to add attributes to the class being mocked?

  • 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. 2026-05-11T06:34:04+00:00Added an answer on May 11, 2026 at 6:34 am

    Update: I’ve just realised that you can actually add attributes to an existing type by using TypeDescriptor.AddAttributes, which can be executed against an instance or a a type:

    Mock<IRepository> repositoryMock = new Mock<IRepository>();  CustomAttribute attribute = new CustomAttribute();  // option #1: to the instance TypeDescriptor.AddAttributes(repositoryMock.Object, attribute );  // option #2: to the generated type TypeDescriptor.AddAttributes(repositoryMock.Object.GetType(), attributes); 

    If you need it, AddAttribute returns a TypeDescriptorProvider that can be passed to TypeDescriptor.RemoveProvider to remove the attributes afterwards.

    Be aware that Attribute.GetCustomAttributes will not find attributes added at runtime in this way. Instead, use TypeDescriptor.GetAttributes.

    Original Answer

    I don’t belive Moq (or any other mocking framework for that matter) supports custom attributes. I do know that Castle Proxy (the framework commonly used to actually create the class) does support it, but there’d be no way to access it through Moq.

    Your best bet is to abstract your method of loading Attributes into an interface (that accepts the Type and the Attribute type) and then mock that.

    Edit: For example:

    public interface IAttributeStrategy {     Attribute[] GetAttributes(Type owner, Type attributeType, bool inherit);     Attribute[] GetAttributes(Type owner, bool inherit); }  public class DefaultAttributeStrategy : IAttributeStrategy {     public Attribute[] GetAttributes(Type owner, Type attributeType, bool inherit)     {         return owner.GetCustomAttributes(attributeType, inherit);     }      public Attribute[] GetAttributes(Type owner, bool inherit)     {         return owner.GetCustomAttributes(inherit);     } } 

    The class that needs the attributes uses an instance of IAttributeStrategy (either through an IoC container, or having it optionally passed into the constructor). Usually it will be a DefaultAttributeStrategy, but you can now mock IAttributeStrategy in order to override the output.

    It might sound convoluted, but adding a layer of abstraction is much easier than attempting to trying to actually mock Attributes.

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

Sidebar

Ask A Question

Stats

  • Questions 89k
  • Answers 89k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You could just run the generator and ignore the models/migrations/fixtures.… May 11, 2026 at 5:52 pm
  • Editorial Team
    Editorial Team added an answer I'm not sure if there is an official one but… May 11, 2026 at 5:52 pm
  • Editorial Team
    Editorial Team added an answer Replace recursion with a loop: private static bool WaitForRestart() {… May 11, 2026 at 5:52 pm

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am currently running into a problem where an element is coming back from
Seemingly simple, but I cannot find anything relevant on the web. What is the
Configuring TinyMCE to allow for tags, based on a customer requirement. My config is
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.