首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >语言-Ext联合-目标运行时不支持默认接口实现

语言-Ext联合-目标运行时不支持默认接口实现
EN

Stack Overflow用户
提问于 2021-01-22 18:12:56
回答 1查看 65关注 0票数 0

我试图在Language-Ext中使用Union类型,但收到错误:“目标运行时不支持默认接口实现”和“类型或命名空间名称'InconsistentFirstName‘找不到”

我的代码如下所示:

代码语言:javascript
复制
[Union]
public interface Inconsistency
{
    public Inconsistency InconsistentFirstName(string localValue, string contactCentreValue);
    public Inconsistency InconsistentSurname(string localValue, string contactCentreValue);

    public string GetFieldName(Inconsistency inconsistency)
        => inconsistency switch
        {
            InconsistentFirstName n => "Name",
            InconsistenctSurname s => "Surname",
            _ => ""
        };
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-22 18:45:23

需要将GetFieldName添加到单独的类文件中。有一个由代码自动生成的分部类,叫做InconsistencyCon,所以我想要实现的东西可以用下面的代码来实现:

代码语言:javascript
复制
[Union]
public abstract partial class Inconsistency
{
    public abstract Inconsistency InconsistentFirstName(string localValue, string remoteValue); 
    public abstract Inconsistency InconsistentSurname(string localValue, string remoteValue);
}

public static partial class InconsistencyCon
{
     public static string FieldName(this Inconsistency inconsistency) =>
         inconsistency switch
         {
             InconsistentFirstName n => "Name",
             InconsistentSurname s   => "Surname",
             _  => ""
         };

     public static string Test(Inconsistency inconsistency)
        {
            if (inconsistency is InconsistentFirstName n)
            {
                var a = n.LocalValue;
                var b = n.FieldName;
                // etc
            }
        }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65843164

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档