C# 通用<;T>;静态方法访问成员方法
我试图让通用静态函数C# 通用<;T>;静态方法访问成员方法,c#,generics,C#,Generics,我试图让通用静态函数Load()调用一个虚拟函数,但显然C#编译器无法识别t中的PostLoad()。我想知道是否有办法实现这一点。有什么想法吗 public abstract class Model<T> where T : new() { public virtual void PostLoad() { } public static T Load() { T bar; //assigning values to bar
Load()
调用一个虚拟函数,但显然C#编译器无法识别t中的PostLoad()
。我想知道是否有办法实现这一点。有什么想法吗
public abstract class Model<T> where T : new()
{
public virtual void PostLoad() { }
public static T Load()
{
T bar;
//assigning values to bar
....
//compilation error: Type `T' does not contain a definition for `PostLoad' and no extension method
bar.PostLoad();
return bar;
}
}
公共抽象类模型,其中T:new()
{
公共虚拟void PostLoad(){}
公共静态T荷载()
{
T形杆;
//为条形图指定值
....
//编译错误:类型“T”不包含“PostLoad”的定义,并且没有扩展方法
bar.PostLoad();
返回杆;
}
}
您的抽象类Model
是定义PostLoad
的类,但typeT
不是T
可以是任何东西,这就是为什么编译器会给您一个错误。因此,在方法Load
中,如果要调用PostLoad
public abstract class Model<T> where T : new()
{
public virtual void PostLoad() { }
public static T Load()
{
Model<T> model = new ...create some type here...
//assigning values
model.PostLoad();
//now what do you return?
}
}
公共抽象类模型,其中T:new()
{
公共虚拟void PostLoad(){}
公共静态T荷载()
{
模型=新建…在此创建一些类型。。。
//赋值
model.PostLoad();
//现在你回来干什么?
}
}
我想你需要再看看你的设计。。。你到底想做什么?你的抽象类
模型是定义PostLoad
的类,但typeT
不是T
可以是任何东西,这就是为什么编译器会给您一个错误。因此,在方法Load
中,如果要调用PostLoad
public abstract class Model<T> where T : new()
{
public virtual void PostLoad() { }
public static T Load()
{
Model<T> model = new ...create some type here...
//assigning values
model.PostLoad();
//now what do you return?
}
}
公共抽象类模型,其中T:new()
{
公共虚拟void PostLoad(){}
公共静态T荷载()
{
模型=新建…在此创建一些类型。。。
//赋值
model.PostLoad();
//现在你回来干什么?
}
}
我想你需要再看看你的设计。。。你到底想做什么?你的抽象类模型是定义PostLoad
的类,但typeT
不是T
可以是任何东西,这就是为什么编译器会给您一个错误。因此,在方法Load
中,如果要调用PostLoad
public abstract class Model<T> where T : new()
{
public virtual void PostLoad() { }
public static T Load()
{
Model<T> model = new ...create some type here...
//assigning values
model.PostLoad();
//now what do you return?
}
}
公共抽象类模型,其中T:new()
{
公共虚拟void PostLoad(){}
公共静态T荷载()
{
模型=新建…在此创建一些类型。。。
//赋值
model.PostLoad();
//现在你回来干什么?
}
}
我想你需要再看看你的设计。。。你到底想做什么?你的抽象类模型是定义PostLoad
的类,但typeT
不是T
可以是任何东西,这就是为什么编译器会给您一个错误。因此,在方法Load
中,如果要调用PostLoad
public abstract class Model<T> where T : new()
{
public virtual void PostLoad() { }
public static T Load()
{
Model<T> model = new ...create some type here...
//assigning values
model.PostLoad();
//now what do you return?
}
}
公共抽象类模型,其中T:new()
{
公共虚拟void PostLoad(){}
公共静态T荷载()
{
模型=新建…在此创建一些类型。。。
//赋值
model.PostLoad();
//现在你回来干什么?
}
}
我想你需要再看看你的设计。。。您到底想做什么?通过添加泛型约束并将虚拟函数移动到基类。编译器将识别PostLoad()
公共抽象类模型
{
公共虚拟void PostLoad(){}
}
公共抽象类模型:模型,其中T:Model,new()
{
公共静态T荷载()
{
T形杆;
//为条形图指定值
....
//编译错误:类型“T”不包含“PostLoad”的定义,并且没有扩展方法
bar.PostLoad();
返回杆;
}
}
通过添加泛型约束并将虚拟函数移动到基类。编译器将识别PostLoad()
公共抽象类模型
{
公共虚拟void PostLoad(){}
}
公共抽象类模型:模型,其中T:Model,new()
{
公共静态T荷载()
{
T形杆;
//为条形图指定值
....
//编译错误:类型“T”不包含“PostLoad”的定义,并且没有扩展方法
bar.PostLoad();
返回杆;
}
}
通过添加泛型约束并将虚拟函数移动到基类。编译器将识别PostLoad()
公共抽象类模型
{
公共虚拟void PostLoad(){}
}
公共抽象类模型:模型,其中T:Model,new()
{
公共静态T荷载()
{
T形杆;
//为条形图指定值
....
//编译错误:类型“T”不包含“PostLoad”的定义,并且没有扩展方法
bar.PostLoad();
返回杆;
}
}
通过添加泛型约束并将虚拟函数移动到基类。编译器将识别PostLoad()
公共抽象类模型
{
公共虚拟void PostLoad(){}
}
公共抽象类模型:模型,其中T:Model,new()
{
公共静态T荷载()
{
T形杆;
//为条形图指定值
....
//编译错误:类型“T”不包含“PostLoad”的定义,并且没有扩展方法
bar.PostLoad();
返回杆;
}
}
我不知道class-level Load()函数应该做什么。但这里的一般规则是类级函数不能调用实例函数,除非它们可以访问对象本身,然后可以调用其函数
我将假设Load()f