获取异常方法未找到:' System.Runtime.CompilerServices.CallSiteBinder

时间:2017-03-09 20:11:40

标签: c# .net-core porting .net-4.6.2

我目前正致力于将.net 4 dll迁移到.net核心。 我移植了我的单元测试但无法使它们工作。

这是dll的project.json



{
  "version": "1.0.0-*",

  "dependencies": {
    "NETStandard.Library": "1.6.1",
  },

  "frameworks": {
    "netstandard1.6": {
      "imports": "dnxcore50"
    }
  }
}




这是我目前正在测试的课程

public class Matcher
{
    public List<ElementMatcher> matchers{ get; set; }

    public bool Match(string item, Dictionary<string, object> elements)
    {

                foreach(var matcher in matchers)
                {
                    var result = matcher.Match(item, elements);
                    if (!result)
                    {
                        return false;
                    }
                }
                return true;

    }
}

public class ElementMatcher
{
    public string attribute { get; set; }
    public IMatcher matcher { get; set; }

    public virtual bool Match(string key, Dictionary<string, object> attributes)
    {
        if (attribute == null)
        {
            return matcher.Match(key);
        }

        if (attributes == null)
        {
            return false;
        }

        object value;
        attributes.TryGetValue(attribute, out value);

        if (value == null)
        {
            return false;
        }

        return matcher.Match((dynamic)value);
    }
}

这是在Matcher类中抛出异常的行:

matcher.Match(item, elements);

这是我的单元测试项目中的当前依赖项:

&#13;
&#13;
<packages>
  <package id="Castle.Core" version="4.0.0" targetFramework="net462" />
  <package id="Microsoft.CSharp" version="4.3.0" targetFramework="net462" />
  <package id="Microsoft.Win32.Primitives" version="4.0.1" targetFramework="net462" />
  <package id="Moq" version="4.7.1" targetFramework="net462" />
  <package id="System.Diagnostics.DiagnosticSource" version="4.0.0" targetFramework="net462" />
  <package id="System.IO" version="4.1.0" targetFramework="net462" />
  <package id="System.IO.FileSystem" version="4.0.1" targetFramework="net462" />
  <package id="System.IO.FileSystem.Primitives" version="4.0.1" targetFramework="net462" />
  <package id="System.IO.FileSystem.Watcher" version="4.0.0" targetFramework="net462" />
  <package id="System.Linq" version="4.1.0" targetFramework="net462" />
  <package id="System.Linq.Expressions" version="4.1.0" targetFramework="net462" />
  <package id="System.Net.Http" version="4.1.0" targetFramework="net462" />
  <package id="System.Net.NameResolution" version="4.0.0" targetFramework="net462" />
  <package id="System.Reflection" version="4.1.0" targetFramework="net462" />
  <package id="System.Runtime" version="4.1.0" targetFramework="net462" />
  <package id="System.Runtime.Extensions" version="4.1.0" targetFramework="net462" />
  <package id="System.Security.Cryptography.Algorithms" version="4.2.0" targetFramework="net462" />
  <package id="System.Security.Cryptography.Encoding" version="4.0.0" targetFramework="net462" />
  <package id="System.Security.Cryptography.Primitives" version="4.0.0" targetFramework="net462" />
  <package id="System.Security.Cryptography.X509Certificates" version="4.1.0" targetFramework="net462" />
  <package id="System.Text.RegularExpressions" version="4.1.0" targetFramework="net462" />
  <package id="System.Threading.Thread" version="4.0.0" targetFramework="net462" />
</packages>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:2)

根据您在评论部分中所说的内容,我认为问题不会因为框架版本之间的某些冲突而发生。您已经说过要将您的库移植到.NET Core中,所以可能并非所有内容都得到完全支持。整个“强制转换为动态,然后决定调用哪种方法”可能会产生问题(并且这不是解决此类问题的一种非常好的方法),所以我尝试做这样的事情:

public interface IMatcher
{
    bool Match(object value);
    bool Match(string key);
    bool Match(DateTime key);
    bool Match(long key);
}

public class MyMatcher : IMatcher
{
    public bool Match(object value)
    {
        if (value is string)
        {
            return Match(value as string);
        } 
        else if (value is DateTime)
        {
            return Match(value as DateTime);
        } 
        else if (value is long)
        {
            return Match(value as long);
        }

        return false;
    }
}

这样,您可以删除动态演员..