我有一个关于退货日期的类:
public class OperationResult<TResult>
{
public TResult Result { get; private set; }
public bool Success { get; private set; }
public string ErrorMessage { get; private set; } = null;
public Exception Exception { get; private set; } = null;
public static OperationResult<TResult> BuildSuccessResult(TResult result)
{
return new OperationResult<TResult> { Success = true, Result = result };
}
public static OperationResult<TResult> BuildFailure(string errorMessage)
{
return new OperationResult<TResult> { Success = false, ErrorMessage = errorMessage };
}
public static OperationResult<TResult> BuildFailure(Exception ex)
{
return new OperationResult<TResult> { Success = false, Exception = ex };
}
public static OperationResult<TResult> BuildFailure(Exception ex, string errorMessage)
{
return new OperationResult<TResult> { Success = false, Exception = ex, ErrorMessage = errorMessage };
}
}
我用这个作为函数的结果:
public async Task<OperationResult<IEnumerable<TResult>>> GetAll<TResult>(string spName)
{
using (var connection = new SqlConnection(configration.ReportingConnection))
{
try
{
connection.Open();
var query = await connection.QueryAsync<TResult>(spName);
if (query != null)
{
var res= OperationResult<IEnumerable<TResult>>().BuildSuccessResult(query);
}
return new OperationResult<IEnumerable<TResult>>().BuildFailure("Error When Getting Date form DateBase");
}
catch (Exception ex)
{
return OperationResult<IEnumerable<TResult>>().BuildFailure(ex,"");
}
finally
{
connection.Close();
}
}
}
但它显示了这行的错误:
不可调用的成员OperationResult不能像方法一样使用。
在行中:
var res= OperationResult<IEnumerable<TResult>>().BuildSuccessResult(query);
或显示此错误
不能使用实例引用访问成员"OperationResult>.BuildFailure(string(";使用类型名称来限定它,而不是AdminPanelGetWay.DAL.Dapper
在行中
return new OperationResult<IEnumerable<TResult>>().BuildFailure("Error When Getting Date form DateBase");
现在有什么问题?我该如何解决这个问题
这是因为您的方法都是静态的。代替
var res= OperationResult<IEnumerable<TResult>>().BuildSuccessResult(query);
您需要删除"((">
var res= OperationResult<IEnumerable<TResult>>.BuildSuccessResult(query);
在类中调用static
方法时,不需要创建此类的对象。
您可以使用类名直接调用类内部的静态方法。(参考此(
将有错误的两句改为以下内容:
//....
var res = OperationResult<IEnumerable<TResult>>.BuildSuccessResult(query);
//....
return OperationResult<IEnumerable<TResult>>.BuildFailure("Error When Getting Date form DateBase");
//....