Anonymous class method call

I am completely new to C # and I apologize if the Question heading does not exactly match the content. But now to my problem:

I have the following construct:

public interface IClass<TEnum> { Dictionary<TEnum, ISecondClass> dictionary { get; } } public abstract class ClassBase<TEnum> : IClass<TEnum> { public abstract Dictionary<TEnum, ISecondClass> dictionary { get; protected set; } } public class ConcreteClass : ClassBase<ConcreteClass.Concrete> { public override Dictionary<Concrete, ISecondClass> dictionary { get; protected set; } public enum Concrete : ulong { } } public class OtherClass : ClassBase<OtherClass.Other> { public override Dictionary<Concrete, ISecondClass> dictionary { get; protected set; } public enum Other : ulong { } } 

My goal is to create an instance of all existing concrete classes based on its enumerations, save all instances in the dictionary, and later call some methods for each object. I'm not sure if this is possible?

I am happy for any hint of it!

+4
source share
3 answers

If I understand what you are trying to do, this is similar to the version of the Multiton Pattern . You may find it helpful to study this.

From Wikipedia example Multi-tone code:

 class FooMultiton { private static readonly Dictionary<object, FooMultiton> _instances = new Dictionary<object, FooMultiton>(); private FooMultiton() {} public static FooMultiton GetInstance(object key) { lock (_instances) { FooMultiton instance; if (!_instances.TryGetValue(key, out instance)) { instance = new FooMultiton(); _instances.Add(key, instance); } } return instance; } } 

This does not apply directly to your class, but since you are looking for clues, I think it should point you in the right direction.

One caveat to the above code: the GetInstance method will change the dictionary if key not found. Personally, I associate the "Get" prefix with read-only methods. I would rename GetInstance or split it into two methods.

I'm not quite sure what you mean by "creating all existing concrete classes based on its enumerations." Can you clarify this?

+3
source

Use Activator.CreateInstance() to create objects of specific classes and save them in the dictionary.

Pass the string classname from Enum and create the objects of the dynamic class. Store them in Dictionary<Enum, ISecondClass>

 myObject = (MyAbstractClass)Activator.CreateInstance("AssemblyName", "TypeName"); or var type = Type.GetType("MyFullyQualifiedTypeName"); var myObject = (MyAbstractClass)Activator.CreateInstance(type); 

When retrieving, based on your enumeration key, you know what type of instance value represents.

+1
source

I do not understand the purpose of the sample code, but you can write something like this:

 public interface IClass { void MethodToDynamicInvoke(); } public abstract class ClassBase<T> : IClass { private Dictionary<Type, List<IClass>> instances = new Dictionary<Type, List<IClass>>(); public ClassBase() { List<IClass> list; if (!instances.TryGetValue(typeof(T), out list)) { list = new List<IClass>(); instances.Add(typeof(T), list); } list.Add(this); } public abstract void MethodToDynamicInvoke(); public void InvokeMetodOnClassesWithSameEnum() { List<IClass> list; if (instances.TryGetValue(EnumType, out list)) { foreach (var instance in list) { instance.MethodToDynamicInvoke(); } } } } public class ConcreteClass : ClassBase<ConcreteClass.Concrete> { public ConcreteClass() : base() { } public override void MethodToDynamicInvoke() { throw new NotImplementedException(); } public enum Concrete : ulong { } } public class OtherClass : ClassBase<OtherClass.Other> { public OtherClass() : base() { } public override void MethodToDynamicInvoke() { throw new NotImplementedException(); } public enum Other : ulong { } } 
0
source

All Articles