I'm having an hierarchy that looks smth like:
public class SomeBaseClass { }
public class Child1: SomeBaseClass { }
public class Child2: SomeBaseClass { }
//Objects hierarchy
public class A
{
public A(B b, SomeBaseClass sbc) { }
}
public class B
{
public B(C c) { }
}
public class C
{
public B(C c, SomeBaseClass sbc) { }
}
...
public class X
{
public X(SomeBaseClass sbc) { }
}
class Program
{
static void Main()
{
var builder = new ContainerBuilder();
builder.RegisterInstance(new Child1()).As<SomeBaseClass>();
var container = builder.Build();
container.Resolve<A>();
//Some work here.
}
}
At some point of time I would like to use Child2 instead of Child1 instance and use it in all hierarchy dependencies. Is there a way to do this without building new Container? It would be perfect to have something like:
public A ResolveWithBinding(IComponentContext cc, SomeBaseClass sbc)
{
return cc.Resolve<A>().WithRegistered<SomeBaseClass>(sbc);
}
UPD: I've found some sort of workaround:
//Registration code
var factory = new SomeBaseClassAncFactory();
builder.Register(() => factory.GetCurrentInstance()).As<SomeBaseClass>();
//Resolve code
public SomeBaseClass GetCurrentInstance()
{
if(StaticClass.SomeProperty=="A")
return new Clild1();
return new Clild2();
}
And I'm afraid that it is not really thread-safe way. And it seems like using static properties is not a "best practice". I hope, there's another solution.
Implement a proxy class that allows you to switch dynamically between Client1 and Client2. For instance:
public class SomeBaseClassProxy : SomeBaseClass
{
private Client1 c1;
private Client2 c2;
public SomeBaseClassProxy(Client1 c1, Client2 c2) {
this.c1 = c1;
this.c2 = c2;
}
private GetClient() {
return StaticClass.SomeProperty == "A" ? c1 : c2;
}
// SomeBaseClass methods
public override void SomeMethod() {
GetClient().SomeMethod();
}
}
Related
I have a system where an object can take a generic configuration object (think flyweight pattern). I also have a subclass which takes a subclassed configuration object.
In order to access properties that are specific to the subclass configuration object, is it better to maintain a second reference to the subclass or cast to the subclass?
e.g.
class Base {
public BaseConf Conf;
public Base(BaseConf C) {
Conf = C;
}
}
class Derived : Base {
public DerivedConf DerConf; //Create an extra reference, no casting
public Derived(DerivedConf DC) : base(DC) {
DerConf = DC;
}
public void PrintName() {
Console.WriteLine(DerConf.Name);
}
}
class BaseConf {
public BaseConf() {}
}
class DerivedConf : BaseConf {
public string Name;
public DerivedConf(string n) : base() {
Name = n;
}
}
vs.
class Base {
public BaseConf Conf;
public Base(BaseConf C) {
Conf = C;
}
}
class Derived : Base {
public Derived(DerivedConf DC) : base(DC) {}
public void PrintName() {
DerivedConf DerConf = Conf as DerivedConf; //Cast, no extra reference
Console.WriteLine(DerConf.Name);
}
}
class BaseConf {
public BaseConf() {}
}
class DerivedConf : BaseConf {
public string Name;
public DerivedConf(string n) : base() {
Name = n;
}
}
Both have an identical output
I wouldn't want to do either of those and you can get around both by making the Base take a generic, like so:
class Base<T> where T : BaseConf
{
public T Conf;
public Base(T C)
{
Conf = C;
}
}
class Derived : Base<DerivedConf>
{
public Derived(DerivedConf DC) : base(DC)
{
}
public void PrintName()
{
Console.WriteLine(Conf.Name);
}
}
static void Main(string[] args)
{
var derived = new Derived(new DerivedConf("Foo"));
derived.PrintName(); // Foo
}
I have a lot of classes that store different types of data but manipulate the data differently. Is there someway I can abstract what class I'm using...and just call the class's methods? I will have one object that I'm using at a given moment, masterclass.
For example I have class1 and class2. Both classes can do .add .subtract...etc.
I want to say...masterclass is now class1. So I can do masterclass.add instead of class1.add. Then change masterclass to class2 and do a masterclass.subtract instead of class1.subtract.
Ok...maybe this is clearer:
class cat
{
String legs="4 legs";
String claws="cat has lots of claws";
public string GetLegs()
{ return legs+claws;
}
}
class bird
{
String legs="2 wings";
String talons="Bird has 2 talons";
public string GetLegs()
{ return legs+talons;
}
}
class animal;
mainfunction()
{
string temp;
animal = cat;
temp = animal.GetLegs();
animal = bird;
temp = animal.getLegs();
}
You could do it in several ways, either you use interfaces, and implement it like for example:
public interface ICalculate {
void Add();
void Subtract();
}
and implement your classes in such a way that they inherit from the interface, like so:
public class SpecificClass : ICalculate {
public void Add() {
// ...
}
public void Subtract() {
// ...
}
}
public class OtherSpecificClass : ICalculate {
public void Add() {
// ...
}
public void Subtract() {
// ...
}
}
or you can use an abstract base class like:
public abstract class AbstractCalculate {
public abstract void Add();
public abstract void Subtract();
}
and implement specific classes like:
public class SpecificCalculate : AbstractCalculate {
public override void Add() {
// ...
}
public override void Subtract() {
// ...
}
}
in the first example you can create your specific classes like:
ICalculate calc1 = new SpecificCalculate();
and call
calc1.Add();
in the second one one, you can use
AbstractCalculate calc11 = new SpecificCalculate();
and call
calc1.Add();
both have a similar way of working, both have their advantages
more info you can find for example on MSDN
Per suggestion of Ed Plunkett, you could have then for example following implementations (lets say for the ICalculate version)
IList<ICalculate> calculations = new List<ICalculate>();
// <-- add specific instances to the list
calculations.Add( new SpecificClass() );
calculations.Add( new OtherSpecificClass() );
// iterate the list
foreach (var calculation in calculations) {
calculation.Add();
}
or to be more specific to your updated question
public interface IAnimal {
int GetLegs();
}
public class Bird : IAnimal {
public int GetLegs() {
return 2;
}
}
public class Cat : IAnimal {
public int GetLegs() {
return 4;
}
}
and the program would use it like
class Program {
static int GetLegs(IAnimal animal) {
return animal.GetLegs();
}
static void Main(string[] args) {
Cat cat = new Cat();
Bird bird = new Bird();
Console.WriteLine( GetLegs( bird ) ); // 2
Console.WriteLine( GetLegs( cat ) ); // 4
}
}
Or like
IList<IAnimal> animals = new List<IAnimal>();
animals.Add( new Cat() );
animals.Add( new Bird() );
int totalLegs = 0;
foreach (var animal in animals) {
totalLegs += animal.GetLegs(); // or totalLegs += GetLegs( animal );
}
Console.WriteLine( totalLegs ); // 6
I am struggling to cast in a tree hierarchy structure below is an example of the class hierarchy structure I would really appreciate if someone can point me in the right direction.
I am unable to cast
var myobj2 = (IR<JB>)JR;
Classes:
public class BASEA{ }
public class B: BASEA{ }
public class C: B{ }
public interface IR<T> { }
public abstract class JR<T> : IR<T> where T : B
{ public abstract void SetRule(); }
public class Action: JB<C>
{
public override void SetRule()
{
//Logic
}
}
public static class RuleLib
{
public static void ApplyTest<T>(T obj, IR<T> JB) where T:B
{
var myobj2 = (IR<JB>)JR; //=> does not cast!
}
}
public class Test
{
[Test]
public void demo()
{
var obj = new B();
var action = new Action();
RuleLib.ApplyRule(obj,action);
}
}
For this to work, your IRule interface needs to be covariant. The example given here shows the following covariance:
IEnumerable<Derived> d = new List<Derived>();
IEnumerable<Base> b = d;
This is basically exactly what you're doing. So in your code all you need to do is write
public interface IRule<out T> { ... }
instead of
public interface IRule<T> { ... }
This makes it so that you can cast from an IRule<U> to IRule<V> where U is a subclass of V (e.g. casting from IRule<ShiftAction> to IRule<Job>).
Suppose I have a class A:
class A
{
void b() {...}
}
and a class B:
class B
{
A m;
}
This way, if I write B x, I can call x.m.b().
What I need is to dynamically create a method b() inside the B class, so I could use it as x.b() (of course, the results from calls x.m.b() and x.b() should be the same).
How can I do it?
There is one generic solution (where you won't have to f.e create delegates for every method from A you want). Unfortunately, It won't be a strongly-typed one. If you want so, please see other answers.
class A
{
public int B()
{
return 1;
}
}
class B : DynamicObject
{
private readonly A m = new A();
private static readonly Lazy<IEnumerable<MethodInfo>> AMethods =
new Lazy<IEnumerable<MethodInfo>>(() =>
{
var type = typeof (A);
return type.GetMethods(
BindingFlags.Instance |
BindingFlags.Public);
});
public override bool TryInvokeMember(
InvokeMemberBinder binder,
object[] args,
out object result)
{
if (base.TryInvokeMember(binder, args, out result))
{
return true;
}
var methods = AMethods.Value;
var method = methods.SingleOrDefault(mth => mth.Name == binder.Name);
// TODO: additional match (arguments type to handle overloads)
if (method == null)
{
result = null;
return false;
}
result = method.Invoke(this.m, args);
return true;
}
public int OtherBMethods()
{
return 2;
}
}
Usage:
var b = new B();
int result = ((dynamic)b).B();
int other = b.OtherBMethods();
or
dynamic b = new B();
int result = b.B();
int other = b.OtherBMethods();
You could do this with delegates, in modern C# this could look like this
public class A
{
public void b() {...}
}
public class B
{
private A m = new A();
public Action b = ()=>m.b();
}
public void Main()
{
new B().b(); // This now invokes your delegates that invokes the b method on it's internal m object
}
Could also just do it with classical methods and simply expose a b method that does the exact same thing, don't see anything special / hard here? If you're trying to accomplish something else you need to clarify your question, like if you want to automate this there are easy compile time (T4 text templates) or at runtime (generating dynamic proxies).
What you are going to do is implement Decorator pattern in C#.
GoF defines Decorator pattern as "Attach additional responsibilities
to an object dynamically. Decorators provide a flexible alternative to
subclassing for extending functionality.
I would like to recommend look throught this article "Understanding and Implementing Decorator Pattern in C#".
I have created a simple example of the Decorator pattern implementation when you decorate Concrete with A and B functionality.
interface IDecorator
{
void Print();
}
class Concrete : IDecorator
{
public void Print()
{
Console.WriteLine("-> Concrete");
}
}
class A : IDecorator
{
IDecorator decorator;
public A(IDecorator decorator)
{
this.decorator = decorator;
}
public void Print()
{
decorator.Print();
Console.WriteLine("-> A");
}
}
class B : IDecorator
{
IDecorator decorator;
public B(IDecorator decorator)
{
this.decorator = decorator;
}
public void Print()
{
decorator.Print();
Console.WriteLine("-> B");
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("concrete object that should be decorated");
var concrete = new Concrete();
concrete.Print();
Console.WriteLine("let's decorate this object with A decorator");
var decoratedWithA = new A(concrete);
decoratedWithA.Print();
Console.WriteLine("let's decorate this object with B decorator");
var decoratedWithB = new B(concrete);
decoratedWithB.Print();
Console.WriteLine("let's decorate concrete with A and B");
var decoratedWithAB = new B(new A(concrete));
decoratedWithAB.Print();
}
}
I have an abstract A class and classes A1 : A, A2 : A, A3 : A. Then i
also have a method named c(). I want to create classes
A1_with_c_method, A2_with_c_method, A3_with_c_methos while leaving A1,
A2 and A3 unharmed. What is the best way to do this? – h8red
You could do something like this:
abstract class A
{
}
class A1 : A
{
}
class A2 : A
{
}
class A3 : A
{
}
#region Not a good idea, because too many classes
class A1_with_c : A1
{
public void c() { }
}
class A2_with_c : A2
{
public void c() { }
}
class A3_with_c : A3
{
public void c() { }
}
#endregion
// Decorate A with the c() method
class BaseDecorator
{
public A Instance { get; private set; }
public BaseDecorator(A instance)
{
Instance = instance;
}
public virtual void c()
{
// do something with instance
}
}
class Decorator : BaseDecorator
{
BaseDecorator decorator;
public Decorator(BaseDecorator decorator)
: base(decorator.Instance)
{
this.decorator = decorator;
}
public override void c()
{
Console.WriteLine("Ok");
}
}
class Program
{
static void Main(string[] args)
{
// not good
new A1_with_c().c();
new A2_with_c().c();
new A3_with_c().c();
// better
var a_with_c = new BaseDecorator(new A1());
a_with_c.c();
// Let's decorate with something interesting
new Decorator(a_with_c).c();
}
}
I agree with the comments that this really seems odd and I'm wondering why you would ever want to do this but here is a possibility for you.
interface IHasMethodb
{
void b();
}
class A : IHasMethodb
{
public void b() { ... }
}
class B : IHasMethodb
{
A m;
public void b() { return m.b(); }
}
Is this what you're trying to do?
It seems like you either want the concept of wrapping a method, which in your example is as simple as:
class A {
public void b() { ... }
}
class B {
A m;
public void b() { m.b(); }
}
Allowing you to:
B x = new B();
x.b();
If you want to be able to "dynamically create" the method then this might be more applicable, using an Action<T> to allow you to do whatever you like with the A instance, without actually exposing it:
class A {
public void b() {...}
}
class B {
A m;
public Action<A> doSomethingWithA;
public void b() {
if (doSomethingWithA != null)
doSomethingWithA(m);
}
}
Then you can:
B x = new B();
x.doSomethingWithA = a => a.b();
x.b();
This is what I want to do in C# (within class Helper - without generic arguments),
List<AbstractClass<dynamic>> data;
public void Add<T>(AbstractClass<T> thing)
{
this.data.Add((AbstractClass<dynamic>) thing);
}
This helper class would take and work with AbstractClass<> objects and give back AbstractClass<> of specific generic type. AbstractClass<T> contains many functions which return T / take in T like public T Invoke().
For Helper class T cannot be known beforehand. The Add<T>(.. thing) function is not in a class of type T.
To be used like this in Helper class's functions,
foreach(var c in data.Where(x => ...))
{
// public T Invoke() { ... } function within AbstractClass<T>
var b = c.Invoke();
// logic
}
This also fails,
List<AbstractClass<object>> data;
public void Add<T>(AbstractClass<T> thing)
{
this.data.Add((AbstractClass<object>) thing);
}
Now I think I can have,
List<dynamic> data; // or List<object> data;
public void Add<T>(AbstractClass<T> thing)
{
this.data.Add(thing);
}
but I want the constraint that List named data has only elements of type like
ConcreteClass : AbstractClass<OtherClass>
So we would know that there is an public T Invoke() function but we do not know what it returns. This is helpful to avoid mistakes of say misspelling Invocke and only knowing at run-time.
I want to avoid casting to dynamic every time to invoke functions that give back generic type T
To do what you want to do you are going to need to use a Contravariant interface
public class Program
{
static void Main()
{
var m = new Helper();
m.Add(new ConcreteClass());
m.Process();
}
class Helper
{
List<IAbstractClass<OtherClassBase>> data = new List<IAbstractClass<OtherClassBase>>();
public void Add(IAbstractClass<OtherClassBase> thing)
{
this.data.Add(thing);
}
public void Process()
{
foreach(var c in data.Where(x => x.ShouldBeProcessed()))
{
var b = c.Invoke();
Console.WriteLine(b.Question);
var castData = b as OtherClass;
if (castData != null)
Console.WriteLine(castData.Answer);
}
}
}
public interface IAbstractClass<out T>
{
bool ShouldBeProcessed();
T Invoke();
}
abstract class AbstractClass<T> : IAbstractClass<T>
{
public bool ShouldBeProcessed()
{
return true;
}
public abstract T Invoke();
}
class ConcreteClass : AbstractClass<OtherClass>
{
public override OtherClass Invoke()
{
return new OtherClass();
}
}
class OtherClassBase
{
public string Question { get { return "What is the answer to life, universe, and everything?"; } }
}
class OtherClass : OtherClassBase
{
public int Answer { get { return 42; } }
}
}
You do not need to tell Add what kind of class you are passing it, all that matters is it derives from the type specified. You could do public void Add(IAbstractClass<object> thing) and every class would work, but Invoke() would only return objects inside the foreach loop.
You need to figure out what is the most derived class you want Invoke() to return and that is what you set as the type in the list.
Maybe this will work for you:
public class Program
{
static void Main()
{
var m1 = new Helper<OtherClass>();
m1.Add(new ConcreteClass());
var m2 = new Helper<int>();
m2.Add(new ConcreteClass2());
}
class Helper<T>
{
List<AbstractClass<T>> data = new List<AbstractClass<T>>();
public void Add<T1>(T1 thing) where T1 : AbstractClass<T>
{
this.data.Add(thing);
}
}
class AbstractClass<T> { }
class OtherClass { }
class ConcreteClass : AbstractClass<OtherClass> { }
class ConcreteClass2 : AbstractClass<int> { }
}