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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T23:53:35+00:00 2026-05-11T23:53:35+00:00

I have a situation where i want to add LinePragmas to CodeDom objects. But

  • 0

I have a situation where i want to add LinePragmas to CodeDom objects. But some code dom objects have the LinePragma property and some don’t.

So I’m wondering if it’s possible to use the dynamic keyword to detect if the property exists on the object (without throwing an exception) and if it does then add the pragma. Here is my current method:

public static T SetSource<T>(this T codeObject, INode sourceNode)
    where T : CodeObject
{
    codeObject.UserData["Node"] = sourceNode.Source;
    dynamic dynamicCodeObject = codeObject;

    // How can I not throw an exception here?
    if (dynamicCodeObject.LinePragma != null)
    {
        dynamicCodeObject.LinePragma = new CodeLinePragma(
        sourceNode.Source.Path.AbsoluteUri,
        sourceNode.Source.StartLine);
    }

    return codeObject;
}

UPDATE:
The solution I went with was to add an extension method called Exists(). I wrote a blog post about it here:
Member Exists Dynamic C# 4.0

The jist was to create an extension method that returns an object that implements DynamicObject’s TryGetMember. It uses reflection to then return true or false. Which allows you to write code like this:

object instance = new { Foo = "Hello World!" };
if (instance.Reflection().Exists().Foo)
{
    string value = instance.Reflection().Call().Foo;
    Console.WriteLine(value);
}
  • 1 1 Answer
  • 3 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-11T23:53:35+00:00Added an answer on May 11, 2026 at 11:53 pm

    You can detect if an object has a property without having to use the dynamic features of C# 4.0 – instead using the reflection features that have been around for a while (I know at least .NET 2.0, not sure about < 2.0)

    PropertyInfo info = codeObject.getType().GetProperty(
        "LinePragma", 
        BindingFlags.Public | BindingFlags.Instance
    )
    

    If it the object does not have the property, then GetProperty() will return null. You can do similar for fields ( GetField() ) and methods ( GetMethod() ).

    Not only that, but once you have the PropertyInfo, you can use it directly to do your set:

    info.SetValue(codeObject, new CodeLinePragma(), null);
    

    If you’re not sure whether the property has a set method, you could take the even safer route:

    MethodInfo method = info.GetSetMethod();
    if(method != null)
        method.Invoke(codeObject, new object[]{ new CodeLinePragma() });
    

    This also gives you the added benefit of being a little more performant over the lookup overhead of the dynamic call (can’t find a reference for that statement, so I’ll just float it out there).

    I suppose that doesn’t answer your question directly, but rather is an alternative solution to accomplish the same goal. I haven’t actually used #4.0 features yet (even though I’m a huge fan of the dynamic typing available in Ruby). It certainly not as clean/readable as the dynamic solution, but if you don’t want to throw an exception it may be the way to go.

    EDIT: as @arbiter points out, “This is valid only for native .net dynamic objects. This will not work for example for IDispatch.”

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

Sidebar

Related Questions

I have a situation where I want certain code to be executed no matter
I have a situation where I want to map a pair of objects to
I have this situation where I want to display a list of Administration objects
I have a situation where I want my program to read in some numbers
i have a situation i want to add a vimeo player in to my
I have situation when i want to add data from column formCompany, formPlace in
I have a situation where I want to add a class to a div
I have a situation like this I want to add a UILabel next to
I have a situation where I want to perform special processing on some Windows
I have a situation where I have an xml file that I don't want

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.