C# 将方法转换为泛型方法?
我创建了一个如下所示的方法C# 将方法转换为泛型方法?,c#,.net,generics,C#,.net,Generics,我创建了一个如下所示的方法 public BOEod CheckCommandStatus(BOEod pBo, IList<string> pProperties) { pBo.isValid = false; if (pProperties != null) { int Num=-1; pBo.GetType().GetProperty(pProperties[0].ToString()).GetValue(pBo, nul
public BOEod CheckCommandStatus(BOEod pBo, IList<string> pProperties)
{
pBo.isValid = false;
if (pProperties != null)
{
int Num=-1;
pBo.GetType().GetProperty(pProperties[0].ToString()).GetValue(pBo, null);
if (ifIntegerGetValue(pBo.GetType().GetProperty(pProperties[0].ToString()).GetValue(pBo, null).ToString(), out Num))
{
if (Num == 1)
pBo.isValid = true;
}
}
return pBo;
}
公共BOEod检查命令状态(BOEod pBo、IList pProperties)
{
pBo.isValid=false;
if(pProperties!=null)
{
int Num=-1;
pBo.GetType().GetProperty(pProperties[0].ToString()).GetValue(pBo,null);
if(ifIntegerGetValue(pBo.GetType().GetProperty(pProperties[0].ToString()).GetValue(pBo,null.ToString(),out Num))
{
如果(Num==1)
pBo.isValid=true;
}
}
返回pBo;
}
我需要转换这个方法,以使它能够接受所有类型的对象(现在我只接受类型为“BOEod”的对象)
由于我是.Net的新手,所以不知道如何使用泛型。我可以用泛型来完成这个任务吗
解决方案如下:
public T CheckCommandStatus<T>(T pBO, Ilist<string> pProperties){..}
public T CheckCommandStatus(T pBO,Ilist pProperties){..}
这里主要的事情是我需要更改传递对象(pBO)的属性并返回它。您需要
boood
来实现一个定义IsValid
的接口
然后,您将向方法添加一个泛型约束,以仅接受实现该接口的对象
public interface IIsValid
{
bool IsValid{get;set;}
}
public T CheckCommandStatus(T pBO、IList pProperties)
其中T:IIsValid{..}
您需要boood
来实现定义是否有效的接口
然后,您将向方法添加一个泛型约束,以仅接受实现该接口的对象
public interface IIsValid
{
bool IsValid{get;set;}
}
public T CheckCommandStatus(T pBO、IList pProperties)
其中T:IIsValid{..}
公共BOEod检查命令状态(T pBo、IList pProperties),其中T:IBOEod
{
pBo.isValid=false;
if(pProperties!=null)
{
int Num=-1;
string propValue=pBo.GetType().GetProperty(PPProperties[0].ToString()).GetValue(pBo,null).ToString();
if(ifIntegerGetValue(propValue,out Num))
{
如果(Num==1)
pBo.isValid=true;
}
}
返回pBo;
}
公共接口IBOEod
{
bool是有效的{get;set;}
}
要传递到此方法的所有类型都必须实现IBOEod
接口。public boood CheckCommandStatus(T pBo,IList pProperties),其中T:IBOEod
public interface IIsValid
{
bool IsValid{get;set;}
}
{
pBo.isValid=false;
if(pProperties!=null)
{
int Num=-1;
string propValue=pBo.GetType().GetProperty(PPProperties[0].ToString()).GetValue(pBo,null).ToString();
if(ifIntegerGetValue(propValue,out Num))
{
如果(Num==1)
pBo.isValid=true;
}
}
返回pBo;
}
公共接口IBOEod
{
bool是有效的{get;set;}
}
要传递到此方法的所有类型都必须实现IBOEod
接口
public interface IIsValid
{
bool IsValid{get;set;}
}