Does C# posses equilivant magic methods like PHP's __get / __set or Python's __getattr__ / __setattr__

Does C# possess some equilivant to :

PHP: magic methods : __get () / __set()
or
Python __getattr__() / __setattr__()

some kind of generic get/set method to handle properties it cannot find

Basically I need to be able to:

class MyClass
{
      private String someValue;
      private String someOtherValue;

      public String magicMethod(String name)
      {
          if (name == "foo")
               return this.someValue;
         
          if (name == "bar")
               return this.someOtherValue;
      }
}

This is a simplistic example.  In general I am looking for a way to create a single point of access into a class where I can then determine what property needs to be returned.  So if C# does not find a property I am looking for it will drop to that single point of entry.

     
LVL 1
aventureAsked:
Who is Participating?
 
dunglaCommented:
You can make it via index of a class. Take a look at my example.

class MagicMethod
{
    private string someValue;
    private string someOtherValue;
    public string this[string index]
    {
        get
        {
            string result = string.Empty;
            if (index == "foo")
                result = someValue;
            else if (index == "bar")
                result = someOtherValue;
            return result;
        }
        set
        {
            if (index == "foo")
                someValue = value;
            else if (index == "bar")
                someOtherValue = value;
        }
    }
}

Hopes this help
0
 
jonorossiCommented:
Because C# is a statically typed language this is not possible, if you remove the method after it has been compiled you will get a MissingMethodException.

You could make all your property accesses through a the this feature like dungla has demonstrated or through a single method. You could hard code each property access or use reflection to make a magic method like the one you describe. If you want some example code for using reflection just let me know.
0
 
aventureAuthor Commented:
I am gonna give dungla a spin in a few minutes and see how that works out.  Ideally I am trying to simulate a property.  I explored just using a method, something like:

MyClass foo = new MyClass()
f.getProperty("someProperty");

Where:

class MyClass
{
    private String myData;

    public String someProperty(String property)
    {
        if (property == "foo")
            return myData;
    }
}


I just didn't like executing the function vs being able to do:

MyClass foo = new MyClass()
f.someProperty;


Specifically the end result would be:

class MyClass
{
      private SomeOtherClass data = new SomeOtherClass();
      
      public Object magicMethodAsProperty(String name)
      {
            if (name == "foo")
                  return data
      }
}

class SomeOtherClass
{
      public String bar = "Hello World!";      
}



MyClass myClass = new MyClass();
myClass.foo.bar

Granted this is simplified example, but the general idea is there.
0
 
jonorossiCommented:
This is not possible with a static language like C# or Java. To call a method/property on a class it must already exist before the type is loaded into memory. One possible way to do this is to dynamically generate a type at runtime but this is very messy and you will not be able to call the properties like you want because the compiler will know that the property doesn't exist because it would get created at runtime. You'd also have to cast the return value from magicMethodAsProperty to type SomeOtherClass.

I don't see the problem with executing the someProperty method in the first example. The other question I have is, if you know exactly what data you have stored within the object why can't you just create properties for these fields?
0
 
aventureAuthor Commented:
I am writing up a simple Object Relational Mapper, so I don't always know what types/columns I will have before hand.  I mean on some level I could just go in an do the necessary rewriting, but I had some time so I thought I would try to play with some ideas.

Dungla's solution would work for some simple simple things, but I managed to find a way to do some of it with a generic.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.