0

I'm currently working on migrating a .net 4 dll to .net core. I'm porting my unit tests but cannot make them work.

This is my project.json for the dll

{
  "version": "1.0.0-*",

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

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

This is the class I'm currently testing

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

This is the line, in Matcher class, throwing exception:

matcher.Match(item, elements);

And this are the current dependencies in my unit tests project:

<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>
Jawen
  • 1,416
  • 1
  • 14
  • 26

1 Answers1

2

Based on what you said on the commentary section, I don't think the problem occurs because of some conflict between framework versions. You've said you're porting your library do .NET Core, so perhaps not everything is fully supported. The whole "cast to dynamic then decide which method to call" might be creating the problem (and it's not a very nice way to solve this kind of problem), so I'd try to do something like this:

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

This way, you can remove the dynamic cast..

Rodrigo Vedovato
  • 1,008
  • 6
  • 11