Link to home
Create AccountLog in
Avatar of aventure
aventure

asked on

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.

     
ASKER CERTIFIED SOLUTION
Avatar of dungla
dungla
Flag of Viet Nam image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of jonorossi
jonorossi

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.
Avatar of aventure

ASKER

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.
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?
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.