I just wanted to throw it there, that sometimes delegates solve these problems, depending on the context.
If you need to call the static method as a kind of factory or initialization method, then you can declare the delegate and pass the static method to the corresponding universal factory or what it needs is a "common class using this static method".
For example:
class Factory<TProduct> where TProduct : new() { public delegate void ProductInitializationMethod(TProduct newProduct); private ProductInitializationMethod m_ProductInitializationMethod; public Factory(ProductInitializationMethod p_ProductInitializationMethod) { m_ProductInitializationMethod = p_ProductInitializationMethod; } public TProduct CreateProduct() { var prod = new TProduct(); m_ProductInitializationMethod(prod); return prod; } } class ProductA { public static void InitializeProduct(ProductA newProduct) {
Unfortunately, you cannot ensure that the class has the correct method, but you can at least compile the time so that the resulting factory method has everything it expects (i.e., an initialization method with a precisely matched signature). This is better than eliminating run-time reflection.
This approach also has some advantages, i.e. you can reuse init methods, use them as instance methods, etc.
Amir Abiri Dec 28 '11 at 15:08 2011-12-28 15:08
source share