相关疑难解决方法(0)

隐式运算符使用接口

我有一个泛型类,我正在尝试实现隐式类型转换.虽然它主要起作用,但它不适用于界面转换.经过进一步调查,我发现存在编译器错误:"来自接口的用户定义转换"适用.虽然我知道在某些情况下应该强制执行,但我正在尝试做的事情似乎是合法的案例.

这是一个例子:

public class Foo<T> where T : IBar
{
    private readonly T instance;

    public Foo(T instance)
    {
        this.instance = instance;
    }
    public T Instance
    {
        get { return instance; }
    }
    public static implicit operator Foo<T>(T instance)
    {
        return new Foo<T>(instance);
    }
}
Run Code Online (Sandbox Code Playgroud)

使用它的代码:

var concreteReferenceToBar = new ConcreteBar();
IBar intefaceReferenceToBar = concreteReferenceToBar;
Foo<ConcreteBar> concreteFooFromConcreteBar = concreteReferenceToBar;
Foo<IBar> fooFromConcreteBar = concreteReferenceToBar;
Foo<IBar> fooFromInterfaceBar = intefaceReferenceToBar; // doesn't work
Run Code Online (Sandbox Code Playgroud)

有没有人知道一个解决方法,或者任何人都能以令人满意的方式解释为什么我不能施展 interfaceReferenceToBar隐式地Foo<IBar>,因为在我的情况下它没有被转换,只包含在Foo中?

编辑: 看起来协方差可能提供救赎.我们希望C#4.0规范允许使用协方差隐式转换接口类型.

c# compiler-construction generics casting implicit-conversion

53
推荐指数
1
解决办法
2万
查看次数

C#编译器错误?为什么这个隐式的用户定义转换没有编译?

给定以下结构:

public struct Foo<T>
{
   public Foo(T obj) { }

   public static implicit operator Foo<T>(T input)
   {
      return new Foo<T>(input);
   }
}
Run Code Online (Sandbox Code Playgroud)

此代码编译:

private Foo<ICloneable> MakeFoo()
{
    string c = "hello";
    return c; // Success: string is ICloneable, ICloneable implicitly converted to Foo<ICloneable>
}
Run Code Online (Sandbox Code Playgroud)

但是这段代码没有编译 - 为什么?

private Foo<ICloneable> MakeFoo()
{
    ICloneable c = "hello";
    return c; // Error: ICloneable can't be converted to Foo<ICloneable>. WTH?
}
Run Code Online (Sandbox Code Playgroud)

c# compiler-construction

32
推荐指数
2
解决办法
2634
查看次数

为什么我不能使用带显式运算符的接口?

我只是想知道是否有人知道你不允许使用隐式或显式运算符的接口的原因?

例如,这会引发编译时错误:

public static explicit operator MyPlayer(IPlayer player)
{
 ...
}
Run Code Online (Sandbox Code Playgroud)

"不允许在接口之间进行用户定义的转换"

谢谢,

.net c# explicit type-conversion

23
推荐指数
1
解决办法
6880
查看次数

无法隐式转换类型'Microsoft.AspNetCore.Mvc.BadRequestObjectResult'

我有一个asp.net core 2.1项目,并且在控制器操作中遇到以下错误:

无法将类型“ Microsoft.AspNetCore.Mvc.BadRequestObjectResult”隐式转换为“ System.Collections.Generic.IList”。存在显式转换(您是否缺少演员表?)

这是我的代码:

[HttpPost("create")]
[ProducesResponseType(201, Type = typeof(Todo))]
[ProducesResponseType(400)]
public async Task<IList<Todo>> Create([FromBody]TodoCreateViewModel model)
{
    if (!ModelState.IsValid)
    {
        return BadRequest(ModelState);   // This is the line that causes the intellisense error
    }

    await _todoRepository.AddTodo(model);
    return await GetActiveTodosForUser();
}


[HttpGet("GetActiveTodosForUser")]
[ProducesResponseType(200)]
[ProducesResponseType(404)]
public async Task<IList<Todo>> GetActiveTodosForUser(string UserId = "")
{
    if (string.IsNullOrEmpty(UserId))
    {
        UserId = HttpContext.User.FindFirstValue(ClaimTypes.Sid);
    }
    return await _todoRepository.GetAll(UserId, false);
}
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

c# asp.net .net-core

4
推荐指数
3
解决办法
9334
查看次数