Is it possible to encapsulate the object itself within a dynamic property?

I ask this question out of curiosity.

Lets say the class Foo is a singleton and I want to call Foo instead of Foo.Instance to access the static object.

Would it be possible to encapsulate the object itself within a dynamic property?
Would C# allow me to create a dynamic property that refers to anything but a property of the said object?

Like so:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>private static Foo _instance;
public static Foo this {
get {
if (_instance == null) {
_instance = new Foo();
}
return _instance;
}
}
public void Bar(){}
</code>
<code>private static Foo _instance; public static Foo this { get { if (_instance == null) { _instance = new Foo(); } return _instance; } } public void Bar(){} </code>
private static Foo _instance;

public static Foo this {
    get {
        if (_instance == null) {
            _instance = new Foo();
        }
        return _instance;
    }
}
public void Bar(){}

this would allow me to call

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Foo.Bar();
</code>
<code>Foo.Bar(); </code>
Foo.Bar();  

instead of

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>Foo.Instance.Bar();
</code>
<code>Foo.Instance.Bar(); </code>
Foo.Instance.Bar();  

If this is possible, then in witch situation would it be viable to encapsulate any kind of object like this whithout using heritage

for example instead of:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class Ploof : List{}
</code>
<code>class Ploof : List{} </code>
class Ploof : List{}  

we would have:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>class Ploof {
private List _list;
public List this { get { return _list; } }
}
</code>
<code>class Ploof { private List _list; public List this { get { return _list; } } } </code>
class Ploof {
    private List _list;
    public List this { get {  return _list; } }
}

7

What you seem to want is a straight-up static class. The class and all its members are simply declared static. For instance:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public static class Foo
{
static Foo()
{
//initialize any state data needed by methods
}
public static void Bar()
{
//...
}
}
//usage, just as you wanted
Foo.Bar();
</code>
<code>public static class Foo { static Foo() { //initialize any state data needed by methods } public static void Bar() { //... } } //usage, just as you wanted Foo.Bar(); </code>
public static class Foo
{
   static Foo()
   {
       //initialize any state data needed by methods
   }

   public static void Bar()
   {
      //...
   }
}

//usage, just as you wanted
Foo.Bar();

The basic problem with this is that everything in the class has to be declared static, and must always be accessed statically. That in turn means that once this code’s used in more than a couple of places, you really can’t ever change its architecture to be more object-oriented. It also usually means that the class can’t have any inheritance hierarchy, because any parent or child would also have to be static, and if the parent’s static and all children are static, you really just have three partial classes. In C# for instance, you can’t derive a static from any other class, and can’t inherit from a static; they’re single, isolated, widely-scoped oddities of your codebase.

Statics in general are to be minimized because, as above, they violate some key principles of SOLID code design, and of object-oriented code in general (in the real world which OOA&D attempts to model, there are very, very few object generalizations that have no parents, no children, and there are and can only ever be one of them).

These are the problems that the Singleton pattern solves, for the most part. The class itself is not static, but it has two or three static members; a constructor so it can self-initialize, and an instance accessor (which may use a specific backing field, or it may be an auto-property). While you still have static state and static availability which are kind of no-nos, the singleton allows you to use its instance as an instance, passing it around between classes and methods as a dependency, with none of those lower-level objects having to know it’s statically scoped or that there can only ever be one instance. Because the class itself isn’t static, if you wanted to, you could implement interfaces or even extend base classes, and you can theoretically derive from it, although that gets very tricky very quickly.

The tradeoff is always referencing the instance when using the singleton in static state. There are cute ways to mitigate it, but I only included the examples below because I thought they were funny; DO NOT USE THESE EVER:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>public class F
{
static F() {instance = new F();}
private F() {...}
public static F oo {get{return instance;}}
public void Bar() {...}
}
...
//FXCop (and most of your fellow coders) will curse your name
F.oo.Bar();
public class Fo
{
static Fo() {instance = new Fo();}
private Fo() {...}
public static F this(int useZeroItsFunny) {get{return instance;}}
public void Bar() {...}
}
//a WTF if there ever was one...
Fo[0].Bar();
</code>
<code>public class F { static F() {instance = new F();} private F() {...} public static F oo {get{return instance;}} public void Bar() {...} } ... //FXCop (and most of your fellow coders) will curse your name F.oo.Bar(); public class Fo { static Fo() {instance = new Fo();} private Fo() {...} public static F this(int useZeroItsFunny) {get{return instance;}} public void Bar() {...} } //a WTF if there ever was one... Fo[0].Bar(); </code>
public class F
{
   static F() {instance = new F();}

   private F() {...}
   public static F oo {get{return instance;}}

   public void Bar() {...}
}

...

//FXCop (and most of your fellow coders) will curse your name
F.oo.Bar();

public class Fo
{
   static Fo() {instance = new Fo();}

   private Fo() {...}
   public static F this(int useZeroItsFunny) {get{return instance;}}

   public void Bar() {...}
}

//a WTF if there ever was one...
Fo[0].Bar();

1

No, it isn’t.

But if you use the singleton often, you can always create local variable with reference to the instance.

But I would consider such heavy use of singleton as code smell and I would question if you really should be using a singleton.

No, there is (as far as I know) currently a limitation in the DLR where the dynamic overrides are not applied to static members. You can do tricks with a variable so that it redirects all calls to a singletone, or even via extension methods so that every object has a .Foo() member you can call a singleton .Bar() on. You can even do tricks with implicit conversion operators so that Ploof looks and acts like a list.

But again, this is all horrible and ill advised – even more than singletons in the first place.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật