Get derived classes of a base class
-
Hi everybody, I am developing a library for my company and encounter a problem. In my code, I need to know and enumerate through all the derived classes of a base class. Are there any way to get the derived classes of a base class by using reflection in C#? Hope your replies, thanks. Lan hue sau ai lan hue heo Lan hue sau doi trong heo ngoai tuoi
-
Hi everybody, I am developing a library for my company and encounter a problem. In my code, I need to know and enumerate through all the derived classes of a base class. Are there any way to get the derived classes of a base class by using reflection in C#? Hope your replies, thanks. Lan hue sau ai lan hue heo Lan hue sau doi trong heo ngoai tuoi
-
Hi everybody, I am developing a library for my company and encounter a problem. In my code, I need to know and enumerate through all the derived classes of a base class. Are there any way to get the derived classes of a base class by using reflection in C#? Hope your replies, thanks. Lan hue sau ai lan hue heo Lan hue sau doi trong heo ngoai tuoi
Can we know why you do this? It's usually a bad idea for code to know derived classes from base classes. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
Can we know why you do this? It's usually a bad idea for code to know derived classes from base classes. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
Hi, Here is the context that I use reflection: - The library has a base class, e.g AbstractAlgorithm, and some derived classes, e.g ConcreteAlgorithm1, ConcreteAlgorithm2, ... We can get an algorithm instance like this: AbstractAlgorithm algo = new ConcreateAlgorithm1(); - I want to provide another way for client to get an algorithm instance by its name like this: AbstractAlgorithm algo = AlgorithmFactory.createInstance("XYZ algorithm"); So I add an abstract method "getName()" to AbstractAlgorithm base class and each concreate algorithm class implements this method to return its name. The "createInstance(string)" method of AlgorithmFactory will use reflection to enumerate through the derived classes of AbstractAlgorithm, call their "getName()" methods to select the right algorithm class. After that, I use .NET class Activator to create an algorithm instance of the selected class. By doing this (instead of using "switch case" statement), the "createInstance(string)" method is unchanged when new concrete algorithm classes are added to the library. Is it a right design? Hope to see your opinion. Lan hue sau ai lan hue heo Lan hue sau doi trong heo ngoai tuoi
-
Hi, Here is the context that I use reflection: - The library has a base class, e.g AbstractAlgorithm, and some derived classes, e.g ConcreteAlgorithm1, ConcreteAlgorithm2, ... We can get an algorithm instance like this: AbstractAlgorithm algo = new ConcreateAlgorithm1(); - I want to provide another way for client to get an algorithm instance by its name like this: AbstractAlgorithm algo = AlgorithmFactory.createInstance("XYZ algorithm"); So I add an abstract method "getName()" to AbstractAlgorithm base class and each concreate algorithm class implements this method to return its name. The "createInstance(string)" method of AlgorithmFactory will use reflection to enumerate through the derived classes of AbstractAlgorithm, call their "getName()" methods to select the right algorithm class. After that, I use .NET class Activator to create an algorithm instance of the selected class. By doing this (instead of using "switch case" statement), the "createInstance(string)" method is unchanged when new concrete algorithm classes are added to the library. Is it a right design? Hope to see your opinion. Lan hue sau ai lan hue heo Lan hue sau doi trong heo ngoai tuoi
How about simply passing the type of the derived class, instead of a string representing the type? Something like
AlgorithmFactory.CreateInstance(typeof(ConcreteAlgorithm1));
Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
How about simply passing the type of the derived class, instead of a string representing the type? Something like
AlgorithmFactory.CreateInstance(typeof(ConcreteAlgorithm1));
Regards Senthil _____________________________ My Blog | My Articles | WinMacro
I think client will prefer short and friendly name to long and hard to remember class name. I just study it from the Java Cryptography Framework. We can get an MD5 hash algorithm like this: MessageDigest md = MessageDigest.getInstance("MD5"); We do not need to know the name of the class that implements the MD5 hash algorithm. Hope to see your opinion. Lan hue sau ai lan hue heo Lan hue sau doi trong heo ngoai tuoi
-
I think client will prefer short and friendly name to long and hard to remember class name. I just study it from the Java Cryptography Framework. We can get an MD5 hash algorithm like this: MessageDigest md = MessageDigest.getInstance("MD5"); We do not need to know the name of the class that implements the MD5 hash algorithm. Hope to see your opinion. Lan hue sau ai lan hue heo Lan hue sau doi trong heo ngoai tuoi
Yeah, it's a tradeoff :- with type names, you get compile time errors if the user passes an invalid type, with strings, you don't. OTOH, like you said, it's more convenient for the users.. Regards Senthil _____________________________ My Blog | My Articles | WinMacro
-
Yeah, it's a tradeoff :- with type names, you get compile time errors if the user passes an invalid type, with strings, you don't. OTOH, like you said, it's more convenient for the users.. Regards Senthil _____________________________ My Blog | My Articles | WinMacro