[英]Cannot Convert 'MyClass' to 'MyClass'
I am trying to create a Stub class for Unit Testing purposes. 我正在尝试为单元测试目的创建一个Stub类。 The Stub points to the EXACT SAME
EmployeeData
class definition, but the compiler thinks they are different, somehow. Stub指向EXACT SAME
EmployeeData
类定义,但是编译器认为它们有所不同。 As a result I keep getting the following message: 结果,我不断收到以下消息:
Cannot implicitly convert type 'MeasurementContracts.Business.Services.EmployeeData' to 'EmployeeData'
无法将类型“ MeasurementContracts.Business.Services.EmployeeData”隐式转换为“ EmployeeData”
What would cause this? 是什么原因造成的?
THE CLASS LOOKS LIKE: 班级风格:
namespace MeasurementContracts.Business.Services
{
/// <summary>A data service model</summary>
public class EmployeeData
{
public int EmployeeId { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string MiddleName { get; set; }
public string EmailAddress { get; set; }
public string KnownAs { get; set; }
public string Login { get; set; }
public string DomainName { get; set; }
public string EmployeeStatusName { get; set; }
public string WorkNumber { get; set; }
public string SystemPersonType { get; set; }
public string PersonType { get; set; }
public string CellPhoneNumber { get; set; }
public string OrganizationOrCostCenter { get; set; }
public string OrganizationalLevel1Description { get; set; }
public string PositionName { get; set; }
public int SupervisorId { get; set; }
public string GetName(bool useKnowAs = true)
{
if (useKnowAs && !string.IsNullOrWhiteSpace(this.KnownAs))
return $"{this.KnownAs} {this.LastName}";
return $"{this.FirstName} {this.LastName}";
}
}
}
THE STUB LOOKS LIKE: STUB看起来像:
namespace MeasurementContracts.UnitTests
{
using OData.Client;
using MeasurementContracts.Business.Services;
using System;
using System.Collections.Generic;
using System.Linq;
/// <summary>A proxy stub</summary>
public class ODataProxyStub : ODataProxy
{
#region <Constructors>
public ODataProxyStub()
{
Entities = new List<EmployeeData>();
}
#endregion
#region <Properties>
public List<EmployeeData> Entities { get; set; }
#endregion
#region <Properties>
public override EmployeeData GetItem<EmployeeData>(string query)
{
// This is NOT working?
return Entities.FirstOrDefault();
}
public override IEnumerable<EmployeeData> GetList<EmployeeData>(string query)
{
throw new NotImplementedException();
}
public override EmployeeData Post<EmployeeData>(string query, IEnumerable<object> parameters)
{
throw new NotImplementedException();
}
#endregion
}
}
THE PROXY BASE LOOKS LIKE: 代理基础外观:
namespace OData.Client
{
public class ODataProxy
{
protected readonly HttpClient Client;
public ODataProxy();
public string BaseUrl { get; set; }
public virtual T GetItem<T>(string query);
public virtual IEnumerable<T> GetList<T>(string query);
public virtual T Post<T>(string query, IEnumerable<object> parameters);
}
}
Actually it doesn't refer to the same class at all. 实际上,它根本不是指同一个类。 In fact, the second
EmployeeData
doesn't refer to any class. 实际上,第二个
EmployeeData
没有引用任何类。 It's the name of the generic. 这是通用名称。
Your ODataProxy
defines a method: 您的
ODataProxy
定义了一个方法:
public virtual IEnumerable<T> GetList<T>(string query);
which the ODataProxyStub
implements using: ODataProxyStub
使用以下ODataProxyStub
实现:
public override IEnumerable<EmployeeData> GetList<EmployeeData>(string query)
This is not a specialized version for the EmployeeData
type (something C# doesn't support), but you simply renamed T
to EmployeeData
. 这不是
EmployeeData
类型的专用版本(C#不支持),但是您只是将T
重命名为EmployeeData
。
Thus the compiler thinks that the generic template parameter EmployeeData
can't be converted to the concrete class EmployeeData
which is of course correct. 因此,编译器认为不能将通用模板参数
EmployeeData
转换为正确的具体类EmployeeData
。
What you might have intended is: 您可能想要的是:
class ODataProxy<T>
{
public virtual IEnumerable<T> GetList(string query)
{
}
}
class ODataProxyStub : ODataProxy<EmployeeData>
{
public override IEnumerable<EmployeeData> GetList(string query)
{
}
}
LINQpad example to reproduce the issue: LINQpad示例重现该问题:
class Data
{
}
class Proxy
{
public virtual T Get<T>()
{
return default(T);
}
}
class Stub : Proxy
{
private Data x = default(Data);
public override Data Get<Data>()
{
return x;
}
}
Cannot implicitly convert type 'UserQuery.Data' to 'Data'
无法将类型'UserQuery.Data'隐式转换为'Data'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.