相关疑难解决方法(0)

更多关于C#中的隐式转换运算符和接口(再次)

好的.我已经读过这篇文章了,我对它如何适用于我的例子感到困惑(下图).

class Foo
{
    public static implicit operator Foo(IFooCompatible fooLike)
    {
        return fooLike.ToFoo();
    }
}

interface IFooCompatible
{
    Foo ToFoo();
    void FromFoo(Foo foo);
}

class Bar : IFooCompatible
{
    public Foo ToFoo()
    {
        return new Foo();   
    }

    public void FromFoo(Foo foo)
    {
    }
}

class Program
{
    static void Main(string[] args)
    {
        Foo foo = new Bar();
        // should be the same as:
        // var foo = (new Bar()).ToFoo();
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经彻底阅读了我链接的帖子.我已阅读C#4规范的第10.10.3节.给出的所有示例都涉及泛型和继承,而上述情况则不然.

任何人都可以解释为什么在这个例子的上下文中不允许这样做

请不要以"因为规范说明"或仅引用规范的形式发布帖子.显然,规范不足以让我理解,否则我不会发布这个问题. …

c# interface implicit-conversion

23
推荐指数
3
解决办法
7747
查看次数

接口,继承,隐式运算符和类型转换,为什么会这样?

我正在使用名为DDay ICal的类库.它是在Outlook日历中实现的iCalendar系统的C#包装器,以及许多更多系统.我的问题来源于我在使用这个系统时所做的一些工作.

这里有3个问题

  • IRecurrencePattern - 接口
  • RecurrencePattern - IRecurrencePattern接口的实现
  • DbRecurPatt - 具有隐式类型运算符的自定义类

IRecurrencePattern:并非显示所有代码

public interface IRecurrencePattern
{
    string Data { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

RecurrencePattern:并非显示所有代码

public class RecurrencePattern : IRecurrencePattern
{
    public string Data { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

DbRecurPatt:并非显示所有代码

public class DbRecurPatt
{
    public string Name { get; set; }
    public string Description { get; set; }

    public static implicit operator RecurrencePattern(DbRecurPatt obj)
    {
        return new RecurrencePattern() { Data = $"{Name} - {Description}" };
    }
}
Run Code Online (Sandbox Code Playgroud)

令人困惑的部分:通过DDay.ICal系统,他们使用ILists来包含日历中每个事件的重复模式集合,自定义类用于从数据库中获取信息,然后通过它转换为重复模式.隐式类型转换运算符. …

c# linq interface data-conversion implicit-conversion

21
推荐指数
2
解决办法
1104
查看次数

无法隐式"包装"IEnumerable

鉴于此类:

public class Wrapper<T>
{
    public Wrapper(T value)
    {
        Value = value;
    }

    public T Value { get; }

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

此代码段无法编译:

IEnumerable<int> value = new [] { 1, 2, 3 };
Wrapper<IEnumerable<int>> wrapper = value;
Run Code Online (Sandbox Code Playgroud)

错误CS0266:无法将类型'System.Collections.Generic.IEnumerable <int>'隐式转换为'Spikes.Implicit.Wrapper <System.Collections.Generic.IEnumerable <int >>'.存在显式转换(您是否错过了演员?)

但是编译器非常满意这个:

Wrapper<IEnumerable<int>> wrapper = new [] { 1, 2, 3 };
Run Code Online (Sandbox Code Playgroud)

为什么?

c#

8
推荐指数
1
解决办法
84
查看次数

将泛型隐式转换为包装器

我想在返回时自动将值包装在通用容器中(我知道这并不总是可取的,但对我的情况来说这是有道理的).例如,我想写:

public static Wrapper<string> Load() {
    return "";
}
Run Code Online (Sandbox Code Playgroud)

我可以通过在Wrapper类中添加以下内容来完成此操作:

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

不幸的是,当我尝试在IEnumerable这里(以及在ideone)转换完整代码时,这会失败:

public class Test {
    public static void Main() {
        string x = "";
        Wrapper<string> xx = x;

        string[] y = new[] { "" };
        Wrapper<string[]> yy = y;

        IEnumerable<string> z = new[] { "" };
        Wrapper<IEnumerable<string>> zz = z; // (!)
    }
}
public sealed class Wrapper<T> {
    private readonly object _value;
    public Wrapper(T …
Run Code Online (Sandbox Code Playgroud)

c# generics implicit-conversion

6
推荐指数
1
解决办法
627
查看次数

为什么我不能在隐式转换中返回接口,但ExpandoObject可以吗?

我有一个DynamicObject,我希望它可以转换为IDictionary,完全相同的方式ExpandoObject.例如,将ExpandoObject强制转换为IDictionary是完全有效的:

dynamic ex = new ExpandoObject ();
ex.Field = "Foobar";
IDictionary<string, object> dict = ex as IDictionary<string, object>;
Console.WriteLine (dict["Field"]);
Run Code Online (Sandbox Code Playgroud)

现在我尝试将它实现到我自己的DynamicObject中:

public class MyDynamicObject : DynamicObject
{
    public Dictionary<string, object> members = new Dictionary<string, object> ();

    public override bool TryGetMember (GetMemberBinder binder, out object result)
    {
        if (members.ContainsKey (binder.Name)) {
            result = members[binder.Name];
            return true;
        }
        else {
            result = null;
            return false;
        }
    }
    public override bool TrySetMember (SetMemberBinder binder, object value)
    {
        this.members.Add (binder.Name, …
Run Code Online (Sandbox Code Playgroud)

c# casting interface dynamic expandoobject

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