Decorator with generic base class - c#

A co-worker of mine asked me last week if it were possible in C# to extend a generic class from its generic parameter. He said it was possible in C++.
What he wanted makes actually sense. He wanted a generic decorator to annotate an arbitrary class with additional information. Something like:
public class Decorator<T> : T
{
public object AdditionalInformation {get:set;}
}
So that he can now use this generic decorator everywhere instead of T.
The most similar thing I could come with was a container class with the original object, the additional information and an implicit conversion.
public class Decorator<T>
{
private readonly T _instance;
public Decorator(T instance)
{
_instance = instance;
}
public T Instance
{
get { return _instance; }
}
public object AdditionalInformation { get; set; }
public static implicit operator T(Decorator<T> deco)
{
return deco._instance;
}
}
But this is not the same because the implicit conversion is only one way. He cannot use it, for example, as a return type of a method because the additional information would be lost after he implicit conversion.
Does anybody have a better idea?

If you can derive all decorable classes from some base class, then you can try to store decorators in that base class and make its info recoverable. Here is sample code that is guaranteed to contain some errors, but you can get the idea.
public class Decorable
{
Dictionary<Type,object> decors = new Dictionary<Type,object>();
public void AddDecorator<D>(D decor) { decors[typeof(D)] = decor; }
public D GetDecorator<D>()
{
object value;
if (decors.TryGetValue(typeof(D), out value))
return (D)value;
else
return default(D);
}
}
public class Decorator<T> where T: class, Decorable
{
private readonly T _instance;
public Decorator(T instance)
{
_instance = instance;
instance.AddDecorator(this);
}
public T Instance
{
get { return _instance; }
}
public object AdditionalInformation { get; set; }
}
// use it like this
Decorator<MyClass> myDecor = myObj.GetDecorator<Decorator<MyClass>>();
If you cannot derive, then you must store info in some static class. But, as wcoenen commented, you would need to clear that info or you'd get memory leaks. Clearing is error prone and not always possible, so it's better to go with the first approach. For example (not thread safe, you must add locking if you plan to use it in multithreaded apps):
static public class Decorators
{
static Dictionary<object,Dictionary<Type,object>> instance = new Dictionary<object,Dictionary<Type,object>>();
public static void AddDecorator<T,D>(this T obj, D decor)
{
Dictionary<Type,object> d;
if (!instance.TryGetValue(obj, out d))
{
d = new Dictionary<Type,object>();
instance.Add(obj, d);
}
d[typeof(D)]=decor;
}
public static D GetDecorator<T,D>(this T obj)
{
// here must be double TryGetValue, but I leave it to you to add it
return (D) instance[obj][typeof(D)];
}
public static T ClearDecorators(this T obj) { instance.remove(obj); }
}
// Decorator<T> code stays the same, but without type constraint

Related

Overrideable property that can be accessed both by instance and by class

I have an inheritance tree with a bunch of different classes. Each of these classes has some static properties that I need acces to from time to time. Sometimes I need the property of a particular class, and sometimes I need the property of the specific class some polymorphic instance turns out to be.
This would be easy in, say, Java (I think). Just make a bunch of static fields (can these be overriden? I'm not sure). But in C#, non-static fields can ONLY be accessed via an instance (naturally), and static fields can ONLY be accessed via their corresponding class (unnaturally).
And, you can't "overload" by, er, staticity. If a class has a static and a non static Foo, doing instance.Foo fails because it is unclear to the compiler which Foo you're referring to even though it's impossible you're referring to the static one since it's disallowed.
Ok, I'll provide some code. Say I have this:
class Base
{
public static readonly string Property = "Base";
}
class Child1 : Base
{
public static readonly new string Property = "Child 1";
}
class Child2 : Base
{
public static readonly new string Property = "Child 2";
}
And then, somewhere:
public void SomeMethod(Base instance)
{
System.Console.WriteLine(instance.Property); // This doesn't work.
}
And somewhere else:
public void SomeOtherMethod()
{
System.Console.WriteLine(Child2.Property);
}
I want something like that, that actually works.
As Peter Duniho said, this can be done with reflection.
For example, these can be defined within the base class:
public const string Property = "Base";
public virtual string InstanceProperty
{
get
{
return (string)this.GetType()
.GetField("Property", BindingFlags.Public | BindingFlags.Static)
.GetValue(null);
}
}
And then each derived class just has to redefine Property using the new keyword.
I think the best you'll do in C# is something like this:
public class BaseClass
{
public virtual string InstanceProperty
{
get { return StaticProperty; }
}
public static string StaticProperty
{
get { return "BaseClass"; }
}
}
public class Derived1Base : BaseClass
{
public override string InstanceProperty
{
get { return StaticProperty; }
}
public new static string StaticProperty
{
get { return "Derived1Base"; }
}
}
public class Derived1Derived1Base : Derived1Base
{
}
public class Derived2Base : BaseClass
{
public override string InstanceProperty
{
get { return StaticProperty; }
}
public new static string StaticProperty
{
get { return "Derived2Base"; }
}
}

Selectively exposing methods based on constructor used?

Is it possible to selectively chose (with a decorator maybe?) what methods are exposed to an object based on a constructor that is called?
For example my class has 2 constructors, an empty one and one that passes in a file path string.
public class MyClass
{
private readonly string _filePath;
public MyClass()
{
}
public MyClass(string filePath)
{
_filePath = filePath
}
public Export()
{
var fi = new FileInfo(_filePath);
}
}
Is it possible that when I create a new MyClass object that only if I use the constructor with the parameter to expose the Export method?
var myClass = new MyClass();
//myClass.Export() not available
var myClass = new MyClass(#"C:\");
//myClass.Export() is available
This is a sign that you should have two different types. Perhaps they both should be sub-types of a parent type (possibly abstract) or perhaps one should simply extend the other.
Then you can construct an instance of the appropriate type based on whether or not you have a string. The type with a string can have an additional method.
public class MyClass
{
public MyClass()
{
}
public void Foo()
{
//todo do stuff
}
}
public class BetterMyClass : MyClass
{
private readonly string _filePath;
public BetterMyClass(string filePath)
{
_filePath = filePath;
}
public void Export()
{
var fi = new FileInfo(_filePath);
}
}
And then your usage works just fine:
var myClass = new MyClass();
//myClass.Export(); //syntax error
var myClass2 = new BetterMyClass(#"C:\");
myClass.Export(); //works
Not directly. You could:
Create a factory method that returns an object of type IMyInterface, and then attempt to cast to the type containing the method you wish to expose. The cast will fail if the object is not the type exposing the method. Or..
Using a dynamic object. The method call will fail at runtime if the method does not exist.
This is possible, just not in the way you are showing here. You would want to create a new class that has only a default constructor and no Export method. Then create a second class that inherits from the first and have a constructor that requires the string and also exposes the Export method.
public class MyClass
{
public MyClass()
{ }
}
public class MyOtherClass : MyClass
{
private readonly string value;
public MyOtherClass(string value)
{
this.value = value;
}
public string Export() { return this.value; }
}
If you absolutely must have the selectivity, which is a silly design decision in my opinion, then you would want to go with a type that is built at runtime using code generation that either does or does not implement the method.
To my knowledge, no, this cannot be done in the way you mean. If you could, the compiler would generally have no way to know if the method in question was valid for the object in question. It would then have to check at run-time. If you called the method when not available, you would receive a runtime exception. You can throw an exception yourself based on a flag set in the constructor.
Ultimately, however, it's likely that what you really want is a subclass which has additional options. That would be a safer way to enable this type of functionality.
You could do this using the factory pattern and returning a different interface
public interface IExportInterface
{
void Export();
}
public interface INoExportInterface
{
//Other methods
}
internal class MyClass : IExportInterface, INoExportInterface
{
private readonly string _filePath;
public MyClass()
{
}
public MyClass(string filePath)
{
_filePath = filePath;
}
public void Export()
{
var fi = new FileInfo(_filePath);
}
}
public class MyClassFactory
{
public static IExportInterface GetMyClass(string filePath)
{
return new MyClass(filePath);
}
public static INoExportInterface GetMyClass()
{
return new MyClass();
}
}

Is it possible to make an object expose the interface of an type parameter?

In C#, is it possible to write something like this:
public class MyClass<T> : T
where T : class, new()
{
}
I know that the above implementation does not compile, but what I am actually trying to achive is implementing some kind of generic wrapper to an unknown type, so that an client can call the wrapper just as he would call the type, provided by the parameter T, instead of calling it using something like wrapper.Instance.SomeMember().
Thanks in advance!
This isn't possible.
In my opinion, I don't think that a wrapper should be implemented using inheritance.
For example, let's say we've an Engine class and you need to implement a FerrariEngine. And you have a Car class.
You're saying that Car should inherit FerrariEngine. It looks terrible for me!
At the end of the day, you're looking to do something like dependency injection using inheritance and, again, this isn't the right path.
My suggestion is don't try to make your life easier: decide an architecture based on rational points.
UPDATE
The OP said in some comment:
I want to make this class to manage instances of objects of type T, so
that the client does not need to take care of when the instances need
to be created.
You don't need to make strange things to get what you want:
public interface IEngine
{
void Start();
}
public sealed class FerrariEngine : IEngine
{
public FerrariEngine()
{
Start();
}
public void Start()
{
}
}
public abstract class Car<TEngine> where TEngine: IEngine, new()
{
public Car()
{
_engine = new Lazy<TEngine>(() => new TEngine());
}
private readonly Lazy<TEngine> _engine;
public TEngine Engine
{
get { return _engine.Value; }
}
}
public class FerrariCar : Car<FerrariEngine>
{
}
Finally, if we create an instance of FerrariCar:
Car<FerrariEngine> myFerrari = new FerrariCar();
The engine will be instantiated and started, without developer intervention!
Check how Lazy<T> and basic generic constraints make the job ;)
In summary:
Using Lazy<T> the engine will be instantiated only when some access the Engine property.
Once the lazy-loaded engine is instantiated, since FerrariEngine implements a parameterless constructor calling Start() itself, it will start the engine.
I believe that this sample illustrates you how you can get what you're looking for and using C# "as is"!
You could have a look at DynamicObject and do something like this:
class Foo<T> : DynamicObject
{
private T _instance;
public Foo(T instance)
{
_instance = instance;
}
public override bool TrySetMember(SetMemberBinder binder, object value)
{
var member = typeof(T).GetProperty(binder.Name);
if (_instance != null &&
member.CanWrite &&
value.GetType() == member.PropertyType)
{
member.SetValue(_instance, value, null);
return true;
}
return false;
}
public override bool TryGetMember(GetMemberBinder binder, out object result)
{
var member = typeof(T).GetProperty(binder.Name);
if (_instance != null &&
member.CanRead)
{
result = member.GetValue(_instance, null);
return true;
}
result = null;
return false;
}
}
class Bar
{
public int SomeProperty { get; set; }
}
class Program
{
static void Main(string[] args)
{
var bar = new Bar();
dynamic thing = new Foo<Bar>(bar);
thing.SomeProperty = 42;
Console.WriteLine(thing.SomeProperty);
Console.ReadLine();
}
}

How can I access a static property of type T in a generic class?

I am trying to accomplish the following scenario that the generic TestClassWrapper will be able to access static properties of classes it is made of (they will all derive from TestClass). Something like:
public class TestClass
{
public static int x = 5;
}
public class TestClassWrapper<T> where T : TestClass
{
public int test()
{
return T.x;
}
}
Gives the error:
'T' is a 'type parameter', which is not valid in the given context.
Any suggestions?
You can't, basically, at least not without reflection.
One option is to put a delegate in your constructor so that whoever creates an instance can specify how to get at it:
var wrapper = new TestClassWrapper<TestClass>(() => TestClass.x);
You could do it with reflection if necessary:
public class TestClassWrapper<T> where T : TestClass
{
private static readonly FieldInfo field = typeof(T).GetField("x");
public int test()
{
return (int) field.GetValue(null);
}
}
(Add appropriate binding flags if necessary.)
This isn't great, but at least you only need to look up the field once...
Surely you can just write this:
public int test()
{
return TestClass.x;
}
Even in a nontrivial example, you can't override a static field so will always call it from your known base class.
Why not just return TestClass.x?
Generics do not support anything related to static members, so that won't work. My advice would be: don't make it static. Assuming the field genuinely relates to the specific T, you could also use reflection:
return (int) typeof(T).GetField("x").GetValue(null);
but I don't recommend it.
Another solution is to simply not make it static, and work with the new() constraint on T to instantiate the object. Then you can work with an interface, and the wrapper can get the property out of any class that implements that interface:
public interface XExposer
{
Int32 X { get; }
}
public class TestClass : XExposer
{
public Int32 X { get { return 5;} }
}
public class XExposerWrapper<T> where T : XExposer, new()
{
public Int32 X
{
get { return new T().X; }
}
}
In fact, you can change that to public static Int32 X on the TestClassWrapper and simply get it out as Int32 fetchedX = XExposerWrapper<TestClass>.X;
Though since whatever code calls this will have to give the parameter T those same constraints, the wrapper class is pretty unnecessary at this point, since that calling code itself could also just execute new T().X and not bother with the wrapper.
Still, there are some interesting inheritance models where this kind of structure is useful. For example, an abstract class SuperClass<T> where T : SuperClass<T>, new() can both instantiate and return type T in its static functions, effectively allowing you to make inheritable static functions that adapt to the child classes (which would then need to be defined as class ChildClass : SuperClass<ChildClass>). By defining protected abstract functions / properties on the superclass, you can make functions that apply the same logic on any inherited object, but customized to that subclass according to its implementations of these abstracts. I use this for database classes where the table name and fetch query are implemented by the child class. Since the properties are protected, they are never exposed, either.
For example, on database classes, where the actual fetching logic is put in one central abstract class:
public abstract class DbClass<T> where T : DbClass<T>, new()
{
protected abstract String FetchQuery { get; }
protected abstract void Initialize(DatabaseRecord row);
public static T FetchObject(DatabaseSession dbSession, Int32 key)
{
T obj = new T();
DatabaseRecord record = dbSession.RetrieveRecord(obj.FetchQuery, key);
obj.Initialize(record);
return obj;
}
}
And the implementation:
public class User : DbClass<User>
{
public Int32 Key { get; private set;}
public String FirstName { get; set;}
public String LastName { get; set;}
protected override String FetchQuery
{ get { return "SELECT * FROM USER WHERE KEY = {0}";} }
protected override void Initialize(DatabaseRecord row)
{
this.Key = DbTools.SafeGetInt(row.GetField("KEY"));
this.FirstName = DbTools.SafeGetString(row.GetField("FIRST_NAME"));
this.LastName = DbTools.SafeGetString(row.GetField("LAST_NAME"));
}
}
This can be used as:
User usr = User.FetchObject(dbSession, userKey);
This is a rather simplified example, but as you see, this system allows a static function from the parent class to be called on the child class, to return an object of the child class.
T is a type, not parameter or variable so you cannot pick any value from any members. Here is a sample code.
public class UrlRecordService
{
public virtual void SaveSlug<T>(T entity) where T : ISlugSupport
{
if (entity == null)
throw new ArgumentNullException("entity");
int entityId = entity.Id;
string entityName = typeof(T).Name;
}
}
public interface ISlugSupport
{
int Id { get; set; }
}
cjk and Haris Hasan have the most-correct answers to the question as asked. However in this comment the OP implies that he is after something else not quite possible in C#: a way to define a contract for a static member in a derived class.
There isn't a way to strictly define this, but it is possible to set up a pattern that may be implied by a base class (or interface); e.g.:
public class TestClass
{
private static int x;
public virtual int StaticX => x;
}
or if not intended to be used directly
public abstract class AbstractTestClass
{
public abstract int StaticX {get;}
}
or (my preference in this contrived example)
public interface ITest
{
int StaticX {get;}
}
Elsewhere, this pattern of a StaticXxx member may be (loosely) associated with implementations that should back the member with static fields (as in TestClass above).
What's kind of fun is that this can be (re)exposed as static by the generic wrapper, because generic statics are isolated to each type used.
public class TestClassWrapper<T> where T : ITest, new()
{
private readonly static T testInstance = new T();
public static int test() => testInstance.x;
}
This uses a new() condition, but an associated static, generic factory pattern for creating ITest (or TestClass or AbstractTestClass) instances may also be used.
However this may not be feasible if you can't have long-lived instances of the class.
In this situation you assume that T is a subclass of TestClass. Subclasses of TestClass will not have the static int x.

Constructor chaining with "this"

Why does the first constructor in ClassA cause the compiler error 'cannot use "this" in member intializer'?
... or how can i get this to work?
Thanks
public sealed class ClassA : IMethodA
{
private readonly IMethodA _methodA;
public ClassA():this(this)
{}
public ClassA(IMethodA methodA)
{
_methodA = methodA;
}
public void Run(int i)
{
_methodA.MethodA(i);
}
public void MethodA(int i)
{
Console.WriteLine(i.ToString());
}
}
public interface IMethodA
{
void MethodA(int i);
}
You are allowed to use the this(...) syntax to invoke another constructor at the same level - however, you cannot use this (the current instance) in this context.
The easiest option here is to duplicate the assignment code (_methodA = methodA).
Another option might be null-coalescing:
public ClassA():this(null)
{}
public ClassA(IMethodA methodA)
{ // defaults to "this" if null
_methodA = methodA ?? this;
}
This is called out in section 10.11.1 of the C# spec
An instance constructor initializer
cannot access the instance being
created. Therefore it is a
compile-time error to reference this
in an argument expression of the
constructor initializer, as is it a
compile-time error for an argument
expression to reference any instance
member through a simple-name.
There is no way to get this to work with an instance constructor because this cannot be accessed. What you could do is make the constructor private, create an initialization method and a static constructor.
public sealed class ClassA : IMethodA {
private ClassA() { }
private void Initialize(IMethodA param) { ... }
public static ClassA Create() {
var v1 = new ClassA();
v1.Initialize(v1);
return v1;
}
public static ClassA Create(IMethodA param) {
var v1 = new ClassA();
v1.Initialize(param);
return v1;
}
}
You're trying to pass the object before it is constructed. Although the compiler could do something sensible in this case, in general that won't work.
Your actual example works if you just do this:
public ClassA()
{
_methodA = this;
}
But you probably want to share more logic, so just use a function.
public ClassA()
{
SetStuff();
_methodA = this;
}
public ClassA(IMethodA methodA)
{
SetStuff();
_methodA = methodA;
}
You can't use the this keyword when chaining constructors essentially because this refers to an object that hasn't been instantiated yet (creation of the object doesn't begin until some (the top-level or base) constructor block has been entered). Moreover, why exactly would you want to do this? It seems rather pointless when you have access to the this keyword everywhere.
I recommend simply using independent constructors as such:
public sealed class ClassA : IMethodA
{
private readonly IMethodA _methodA;
public ClassA()
{
_methodA = this;
}
public ClassA(IMethodA methodA)
{
_methodA = methodA;
}
}
Perhaps I misunderstand what you're trying to do, but hopefully that will solve the issue for you.

Categories