gpt4 book ai didi

c# - 无法将 'MyClass' 转换为 'MyClass'

转载 作者:太空宇宙 更新时间:2023-11-03 21:00:24 26 4
gpt4 key购买 nike

我正在尝试为单元测试目的创建一个 stub 类。 Stub 指向完全相同的 EmployeeData 类定义,但编译器认为它们是不同的,不知何故。结果,我不断收到以下消息:

Cannot implicitly convert type 'MeasurementContracts.Business.Services.EmployeeData' to 'EmployeeData'

  • “EmployeeData”没有其他定义
  • 出于某种原因,它“认为”有 2

什么会导致这种情况?

enter image description here

类看起来像:

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}";
}

}
}

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
}
}

代理基础看起来像:

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);
}
}

最佳答案

其实根本不是指同一个类。事实上,第二个EmployeeData 没有引用任何 类。这是泛型的名称。

您的 ODataProxy 定义了一个方法:

public virtual IEnumerable<T> GetList<T>(string query);

ODataProxyStub 实现使用:

public override IEnumerable<EmployeeData> GetList<EmployeeData>(string query)

不是 EmployeeData 类型的专门版本(C# 不支持),但您只是将 T 重命名为 员工数据

因此编译器认为通用模板参数 EmployeeData 不能转换为具体类 EmployeeData 这当然是正确的。


您可能想要的是:

class ODataProxy<T>
{
public virtual IEnumerable<T> GetList(string query)
{
}
}

class ODataProxyStub : ODataProxy<EmployeeData>
{
public override IEnumerable<EmployeeData> GetList(string query)
{
}
}

重现问题的 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'

关于c# - 无法将 'MyClass' 转换为 'MyClass',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46055480/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com