C# 如何初始化泛型类的T类型对象
我有一个泛型代理类,它包含T类型的对象,也是一个类。我想创建一个T的对象C# 如何初始化泛型类的T类型对象,c#,generics,C#,Generics,我有一个泛型代理类,它包含T类型的对象,也是一个类。我想创建一个T的对象 class Proxy<T>: IClient { T _classObj; public Proxy() { this._classObj = //create new instance of type T } } 类代理:IClient { T_classObj; 公共代理() { 这.\u classObj=//创建T类型的新实例 } } 您
class Proxy<T>: IClient
{
T _classObj;
public Proxy()
{
this._classObj = //create new instance of type T
}
}
类代理:IClient
{
T_classObj;
公共代理()
{
这.\u classObj=//创建T类型的新实例
}
}
您可以使用该类型的默认值(对于引用类型,默认值为null
,对于值类型,默认值为:)
或者应用,强制类型T
具有默认的无参数构造函数
class Proxy<T>: IClient where T: new()
{
T _classObj;
public Proxy() {
_classObj = new T();
}
}
类代理:IClient,其中T:new()
{
T_classObj;
公共代理(){
_classObj=新的T();
}
}
您可以使用该类型的默认值(对于引用类型,默认值为null
,对于值类型,默认值为:)
或者应用,强制类型T
具有默认的无参数构造函数
class Proxy<T>: IClient where T: new()
{
T _classObj;
public Proxy() {
_classObj = new T();
}
}
类代理:IClient,其中T:new()
{
T_classObj;
公共代理(){
_classObj=新的T();
}
}
您可以使用该类型的默认值(对于引用类型,默认值为null
,对于值类型,默认值为:)
或者应用,强制类型T
具有默认的无参数构造函数
class Proxy<T>: IClient where T: new()
{
T _classObj;
public Proxy() {
_classObj = new T();
}
}
类代理:IClient,其中T:new()
{
T_classObj;
公共代理(){
_classObj=新的T();
}
}
您可以使用该类型的默认值(对于引用类型,默认值为null
,对于值类型,默认值为:)
或者应用,强制类型T
具有默认的无参数构造函数
class Proxy<T>: IClient where T: new()
{
T _classObj;
public Proxy() {
_classObj = new T();
}
}
类代理:IClient,其中T:new()
{
T_classObj;
公共代理(){
_classObj=新的T();
}
}
如果T
是一个类,并且它保证它有一个new()
操作符:
class Proxy<T> : IClient where T : class, new() {
T _classObj;
public Proxy() {
this._classObj = new T();
}
}
更新:
对于在T
上调用方法,存在一些不同的情况。但是,根据问题和评论,我假设T
是一个类
,它有一个new()
操作符。此外,它还实现了IGetDataImplementer
,它有一个名为GetData
的方法。因此,我们可以:
interface IGetDataImplementer{
object GetData();
}
class Proxy<T> : IClient where T : class, IGetDataImplementer, new() {
T _classObj;
public Proxy() {
this._classObj = new T();
var data = this._classObj.GetData();
}
}
接口IGETDATA实现程序{
对象GetData();
}
类代理:IClient,其中T:class,IGetDataImplementer,new(){
T_classObj;
公共代理(){
这个._classObj=new T();
var data=这个;
}
}
如果T
是一个类,并且它保证它有一个new()
操作符:
class Proxy<T> : IClient where T : class, new() {
T _classObj;
public Proxy() {
this._classObj = new T();
}
}
更新:
对于在T
上调用方法,存在一些不同的情况。但是,根据问题和评论,我假设T
是一个类
,它有一个new()
操作符。此外,它还实现了IGetDataImplementer
,它有一个名为GetData
的方法。因此,我们可以:
interface IGetDataImplementer{
object GetData();
}
class Proxy<T> : IClient where T : class, IGetDataImplementer, new() {
T _classObj;
public Proxy() {
this._classObj = new T();
var data = this._classObj.GetData();
}
}
接口IGETDATA实现程序{
对象GetData();
}
类代理:IClient,其中T:class,IGetDataImplementer,new(){
T_classObj;
公共代理(){
这个._classObj=new T();
var data=这个;
}
}
如果T
是一个类,并且它保证它有一个new()
操作符:
class Proxy<T> : IClient where T : class, new() {
T _classObj;
public Proxy() {
this._classObj = new T();
}
}
更新:
对于在T
上调用方法,存在一些不同的情况。但是,根据问题和评论,我假设T
是一个类
,它有一个new()
操作符。此外,它还实现了IGetDataImplementer
,它有一个名为GetData
的方法。因此,我们可以:
interface IGetDataImplementer{
object GetData();
}
class Proxy<T> : IClient where T : class, IGetDataImplementer, new() {
T _classObj;
public Proxy() {
this._classObj = new T();
var data = this._classObj.GetData();
}
}
接口IGETDATA实现程序{
对象GetData();
}
类代理:IClient,其中T:class,IGetDataImplementer,new(){
T_classObj;
公共代理(){
这个._classObj=new T();
var data=这个;
}
}
如果T
是一个类,并且它保证它有一个new()
操作符:
class Proxy<T> : IClient where T : class, new() {
T _classObj;
public Proxy() {
this._classObj = new T();
}
}
更新:
对于在T
上调用方法,存在一些不同的情况。但是,根据问题和评论,我假设T
是一个类
,它有一个new()
操作符。此外,它还实现了IGetDataImplementer
,它有一个名为GetData
的方法。因此,我们可以:
interface IGetDataImplementer{
object GetData();
}
class Proxy<T> : IClient where T : class, IGetDataImplementer, new() {
T _classObj;
public Proxy() {
this._classObj = new T();
var data = this._classObj.GetData();
}
}
接口IGETDATA实现程序{
对象GetData();
}
类代理:IClient,其中T:class,IGetDataImplementer,new(){
T_classObj;
公共代理(){
这个._classObj=new T();
var data=这个;
}
}
那么问题出在哪里?例如,代理示例=新代理(5);//如果T,比如说,是INTPLESE,不要在问题标题中包含关于所用语言的信息,除非没有它就没有意义。标记就是为了这个目的。@Javad_Amiry我想要这样的_classObj=new _classObj()_classobject.GetData();这就是我想要的,那么问题出在哪里呢?例如,代理样本=新代理(5);//如果T,比如说,是INTPLESE,不要在问题标题中包含关于所用语言的信息,除非没有它就没有意义。标记就是为了这个目的。@Javad_Amiry我想要这样的_classObj=new _classObj()_classobject.GetData();这就是我想要的,那么问题出在哪里呢?例如,代理样本=新代理(5);//如果T,比如说,是INTPLESE,不要在问题标题中包含关于所用语言的信息,除非没有它就没有意义。标记就是为了这个目的。@Javad_Amiry我想要这样的_classObj=new _classObj()_classobject.GetData();这就是我想要的,那么问题出在哪里呢?例如,代理样本=新代理(5);//如果T,比如说,是INTPLESE,不要在问题标题中包含关于所用语言的信息,除非没有它就没有意义。标记就是为了这个目的。@Javad_Amiry我想要这样的_classObj=new _classObj()_classobject.GetData();这就是我想要的,怎么做