tags:

views:

140

answers:

6

Is it possible to create a "catchall" getter property in C#?

class xyzFactory {

   public static object this.*(string name){
       get { return new name();}
  }
}

In PHP you would write something like

//static getters are new to 5.3, so I didn't put it here.

class xyzFactory{
    public _get($name){ return $name();}
}
+3  A: 

No, you can't do that in C#. C# is a compiled language and statically resolves method slots at compile time. It doesn't support passing the property name as string or things like that.

Mehrdad Afshari
+1 for explanation; I usually deserve downvotes for hopeless answers
Rubens Farias
Vitaliy: I disagree. C# 4.0 does not support "catchall properties" as demonstrated in the question. You cannot do `xyzFactory.HelloWorld`, even in C# 4.0. What C# 4.0 supports is essentially dynamically resolving method calls by *name* ONLY on things typed `dynamic`. It's not changing nature of the type itself. Just a syntactic sugar to call a method on an interface with appropriate parameters.
Mehrdad Afshari
Vitaliy: I would be happy to see the code so that I can call an arbitrarily named static property as the question shows.
Mehrdad Afshari
Oh, sorry, I didn't noticed it's about STATIC properties. I'm withdrawing my -1 vote. Anyway you can achieve this with a hack likexyz.Instance.HelloWorld. But generally you are right - C# by nature does not support dynamic STATIC properties resolving.
Vitaliy Liptchinsky
Vitaliy: I'd argue that even for instance methods, `dynamic` does not add the ability to add a property to a concrete CLR class type. You'll have to do something like `dynamic obj = new xyzFactory();` in which you are explicitly casting the object to `dynamic` at **call site**. Again, you won't be able to do `new xyzFactory().SomeRandomProperty`.
Mehrdad Afshari
Hmmm, in case of instance property you can achieve this if xyzFactory is derived from DynamicObject class.
Vitaliy Liptchinsky
Vitaliy: No, you can't do that solely by inheriting from `DynamicObject`. You always need to cast the variable to `dynamic` at call site.
Mehrdad Afshari
Mehrdad: Yes, but static Instance property I'm suggesting is of type dynamic
Vitaliy Liptchinsky
+8  A: 

Not in C# 3. In C# 4.0 you could achieve something like this with expando properties and the dynamic keyword.

Yann Schwartz
A: 

You could use the property pattern to implement this, as others have said C# won't currently help you implement it though

jk
A: 

The closest you can get is overload index operator ([]). At least until C# 4.0 is out

Sergej Andrejev
+1  A: 

You can achieve this with a hack like

xyzFactory.Instance.Name

where static Instance property is of type dynamic

And make you xyzFactory derived from DynamicObject class.

public xyzFactory : DynamicObject
{
     private static xyzFactory _instance = new xyzFactory();

     private xyzFactory(){}

     pubic static dynamic Instance
     {
          get{return _instance;}
     }

     public override bool  TryGetMember(GetMemberBinder binder, out object result){...}
}
Vitaliy Liptchinsky
too bad it only works in C#4.0
Justin Alexander
A: 

You may be able to do this with LinFu. It uses a dynamic Proxy to allow for Duck Typing and Late Binding, Ruby-style Mixins

Maslow