来自泛型函数的静态方法调用
Static method call from generic function
是否可以从通用函数调用 static
方法?
我需要做这样的事情:
public static T CreateNewProject<T, V>(string token, string projectName)
where V : IProject<T>
{
V.LoadProject();
}
其中 LoadProject()
必须是某些 class 的某些 static
方法。
你不能用约束来做到这一点,但你可以使用 Reflection
。在这种情况下,这似乎是唯一的选择:
typeof(V).GetMethod("LoadProject", BindingFlags.Static | BindingFlags.Public)
.Invoke(null);
你可以做的是不使用静态 类 而是使用单例:
public interface ICanLoadProject
{
void LoadProject(string token, string projectName);
}
实施:
public class SampleProjectLoader : ICanLoadProject
{
private static SampleProjectLoader instance = new SampleProjectLoader();
private SampleProjectLoader(){}
public static SampleProjectLoader Instance { get{ return instance; } }
public void LoadProject(string token, string projectName)
{ /*Your implementation*/ }
}
那么泛型适用于:
where V : ICanLoadProject
之前访问方法的代码只需要做:
SampleProjectLoader.Instance.LoadProject("token","someName");
方法可以是:
public static class ExtensionMethods
{
public static T CreateNewProject<T, V>(this V loader, string token, string projectName) where V : ICanLoadProject
{
loader.LoadProject(token, projectName);
return default(T); //no clue what you want to return here
}
}
并且会被称为:
static void Main(string[] args)
{
object result = SampleProjectLoader.Instance.CreateNewProject<object, SampleProjectLoader>("token", "someName");
}
或者更好:
public static class ExtensionMethods
{
public static T CreateNewProject<T>(this ICanLoadProject loader, string token, string projectName)
{
loader.LoadProject(token, projectName);
return default(T); //no clue what you want to return here
}
}
并调用:
SampleProjectLoader.Instance.CreateNewProject<object>("token", "someName");
是否可以从通用函数调用 static
方法?
我需要做这样的事情:
public static T CreateNewProject<T, V>(string token, string projectName)
where V : IProject<T>
{
V.LoadProject();
}
其中 LoadProject()
必须是某些 class 的某些 static
方法。
你不能用约束来做到这一点,但你可以使用 Reflection
。在这种情况下,这似乎是唯一的选择:
typeof(V).GetMethod("LoadProject", BindingFlags.Static | BindingFlags.Public)
.Invoke(null);
你可以做的是不使用静态 类 而是使用单例:
public interface ICanLoadProject
{
void LoadProject(string token, string projectName);
}
实施:
public class SampleProjectLoader : ICanLoadProject
{
private static SampleProjectLoader instance = new SampleProjectLoader();
private SampleProjectLoader(){}
public static SampleProjectLoader Instance { get{ return instance; } }
public void LoadProject(string token, string projectName)
{ /*Your implementation*/ }
}
那么泛型适用于:
where V : ICanLoadProject
之前访问方法的代码只需要做:
SampleProjectLoader.Instance.LoadProject("token","someName");
方法可以是:
public static class ExtensionMethods
{
public static T CreateNewProject<T, V>(this V loader, string token, string projectName) where V : ICanLoadProject
{
loader.LoadProject(token, projectName);
return default(T); //no clue what you want to return here
}
}
并且会被称为:
static void Main(string[] args)
{
object result = SampleProjectLoader.Instance.CreateNewProject<object, SampleProjectLoader>("token", "someName");
}
或者更好:
public static class ExtensionMethods
{
public static T CreateNewProject<T>(this ICanLoadProject loader, string token, string projectName)
{
loader.LoadProject(token, projectName);
return default(T); //no clue what you want to return here
}
}
并调用:
SampleProjectLoader.Instance.CreateNewProject<object>("token", "someName");