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

Ask A Question

Stats

  • Questions 88k
  • Answers 88k
  • 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'll have to decide whether you will accept "42" to… May 11, 2026 at 5:41 pm
  • Editorial Team
    Editorial Team added an answer I'd start by checking your environment. I suspect you've exported… May 11, 2026 at 5:41 pm
  • Editorial Team
    Editorial Team added an answer Reading this article by John Resig on JavaScript Benchmark Quality… May 11, 2026 at 5:41 pm

Related Questions

Does C# have anything like Python's __getattr__ ? I have a class with many
I've written some C code that I would like to port to python, as
I want to use the new and delete operators for creating and destroying my
I am going to need to create a small windows application that stores basic
I am a C/C++ programmer professionally, but I've created a couple of personal web

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.