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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:32:49+00:00 2026-05-27T10:32:49+00:00

When using reflection, fields, properties, indexers, and parameters can be examined for the DynamicAttribute

  • 0

When using reflection, fields, properties, indexers, and parameters can be examined for the DynamicAttribute attribute in order to determine that they have a dynamic type. However, this doesn’t work for Methods – even though they return ‘dynamic’, they have NO attributes and their return type is ‘object’ (and also has no attributes). Visual Studio does this for intellisense for methods in external assemblies… can it be done with reflection?

For example, the code below produces this output:

dynamicField is dynamic
DynamicProperty is dynamic
Item is dynamic
DynamicMethod is NOT dynamic!!
dynamicParameter is dynamic

Example code:

using System;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace ReflectDynamics
{
    class Program
    {
        static void Main()
        {
            var test = typeof(Test);
            CheckDynamic(test.GetField("dynamicField"));
            CheckDynamic(test.GetProperty("DynamicProperty"));
            CheckDynamic(test.GetProperty("Item"));
            MethodInfo dynamicMethod = test.GetMethod("DynamicMethod");
            CheckDynamic(dynamicMethod);
            CheckDynamic(dynamicMethod.GetParameters()[0]);
            Console.ReadKey();
        }

        static void CheckDynamic(MemberInfo memberInfo)
        {
            bool isDynamic = memberInfo.GetCustomAttributes(typeof(DynamicAttribute), true).Length > 0;
            Console.WriteLine(memberInfo.Name + (isDynamic ? " is dynamic" : " is NOT dynamic!!"));
        }
        static void CheckDynamic(ParameterInfo parameterInfo)
        {
            bool isDynamic = parameterInfo.GetCustomAttributes(typeof(DynamicAttribute), true).Length > 0;
            Console.WriteLine(parameterInfo.Name + (isDynamic ? " is dynamic" : " is NOT dynamic!!"));
        }
    }

    class Test
    {
        public dynamic dynamicField;
        public dynamic DynamicProperty { get { return dynamicField; } }
        public dynamic this[int index] { get { return dynamicField; } }
        public dynamic DynamicMethod(dynamic dynamicParameter) { return dynamicField; }
    }
}
  • 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-27T10:32:50+00:00Added an answer on May 27, 2026 at 10:32 am

    This is because you need to check the Attributes on the ReturnTypeCustomAttributes from the method.

    Modify your CheckDynamic method like so:

    static void CheckDynamic(MemberInfo memberInfo)
    {
        bool isDynamic = memberInfo.GetCustomAttributes(typeof(DynamicAttribute), true).Length > 0;
    
        var methodInfo = (memberInfo as MethodInfo);
        if (methodInfo != null)
        {
            isDynamic = methodInfo.ReturnTypeCustomAttributes.GetCustomAttributes(typeof(DynamicAttribute), true).Length > 0;
        }
    
        Console.WriteLine(memberInfo.Name + (isDynamic ? " is dynamic" : " is NOT dynamic!!"));
    }
    

    This probably needs some defensive coding, but it’s just a quick and dirty proof of concept.

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

Sidebar

Related Questions

Using reflection, how can I get all types that implement an interface with C#
I'm saving a properties file by using reflection to loop through a class's fields
I am using the Reflection classes in order to get all the fields inside
I'm using reflection to access and store properties and fields. However, to avoid having
When using reflection it is possible to obtain the call stack (apart from that
I'm using reflection to loop through a Type 's properties and set certain types
I thought this should be obvious but I can't find it. Now that fields
Via reflection I have found a lists of properties from x POJO classes which
i am using reflection to copy values of fields from object of Class A
Using reflection you can get pretty much everything relating to a class. You can

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.