A
Andrus
I know entity class name as string ( e.q. "Customer", "Product")
I need to invoke static method in this entity class.
I tried the following code but got compile error shown in comment.
How to invoke static method when type name is known as string ?
Andrus.
using System.Collections.Generic;
using System;
class TestApplication {
static void Main() {
FindByTypeName("Customer");
FindByTypeName("Product");
}
static object[] FindByTypeName(string typename) {
Type t = Type.GetType(typename);
ActiveRecordBase<object> entity = (ActiveRecordBase<object>)
System.Activator.CreateInstance(t);
// Error: Static member 'ActiveRecordBase<object>.FindAll()' cannot be
// accessed with an instance reference; qualify it with a type name
//instead
return entity.FindAll();
}
}
class Customer : ActiveRecordBase<Customer> {}
class Product : ActiveRecordBase<Product> {}
public class ActiveRecordBase<T> {
public static T[] FindAll() {
// method implementation skipped
return null;
}
}
I need to invoke static method in this entity class.
I tried the following code but got compile error shown in comment.
How to invoke static method when type name is known as string ?
Andrus.
using System.Collections.Generic;
using System;
class TestApplication {
static void Main() {
FindByTypeName("Customer");
FindByTypeName("Product");
}
static object[] FindByTypeName(string typename) {
Type t = Type.GetType(typename);
ActiveRecordBase<object> entity = (ActiveRecordBase<object>)
System.Activator.CreateInstance(t);
// Error: Static member 'ActiveRecordBase<object>.FindAll()' cannot be
// accessed with an instance reference; qualify it with a type name
//instead
return entity.FindAll();
}
}
class Customer : ActiveRecordBase<Customer> {}
class Product : ActiveRecordBase<Product> {}
public class ActiveRecordBase<T> {
public static T[] FindAll() {
// method implementation skipped
return null;
}
}