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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T05:06:21+00:00 2026-05-11T05:06:21+00:00

Does C# have anything like Python’s __getattr__ ? I have a class with many

  • 0

Does C# have anything like Python’s __getattr__?

I have a class with many properties, and they all share the same accessor code. I would like to be able to drop the individual accessors entirely, just like in Python.

Here’s what my code looks like now:

class Foo {     protected bool Get(string name, bool def)     {         try {             return client.Get(name);         } catch {             return def;         }     }      public bool Bar     {         get { return Get('bar', true); }         set { client.Set('bar', value); }     }      public bool Baz     {         get { return Get('baz', false); }         set { client.Set('baz', value); }     } } 

And here’s what I’d like:

class Foo {     public bool Get(string name)     {         try {             return client.Get(name);         } catch {             // Look-up default value in hash table and return it         }     }      public void Set(string name, object value)     {         client.Set(name, value)     } } 

Is there any way to achieve this in C# without calling Get directly?

Thanks,

  • 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-11T05:06:21+00:00Added an answer on May 11, 2026 at 5:06 am

    No. Although C# supports reflection, it is read-only (for loaded assemblies). That means you can’t change any methods, properties, or any other metadata. Although you could create a dynamic property, calling it wouldn’t be very convenient – it would be even worse than using your Get method. Aside from using a Dictionary<string, object> and an indexer for your class, there’s not much else you can do. Anyway, isn’t doing a dictionary better if you have that many properties?

    Python doesn’t check if an attribute exists at ‘compile-time’ (or at least load-time). C# does. That’s a fundamental difference between the two languages. In Python you can do:

    class my_class:     pass  my_instance = my_class() my_instance.my_attr = 1 print(my_instance.my_attr) 

    In C# you wouldn’t be able to do that because C# actually checks if the name my_attr exists at compile-time.

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

Sidebar

Related Questions

Python has IPython .. does OCaml have anything similar? I'd very much like to
Does ASP.NET MVC have anything like the RESTful out-of-the-box features like Ruby on Rails
Does * have a special meaning in Python as it does in C? I
Using C# .NET 2.0, I have a composite data class that does have the
I have a bash script that does several tasks, including python manage.py syncdb on
Both are mathematical values, however the float does have more precision. Is that the
Does anyone have any recommendations of tools that can be of assistance with moving
Does C# have built-in support for parsing strings of page numbers? By page numbers,
Does anyone have a definitive answer to whether Sql Server Management Objects is compatible
Does anyone have the secret formula to resizing transparent images (mainly GIFs) without ANY

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.