Search code examples
c#nugetextension-methodsusingdnx50

How do you use extensions defined in a nuget package?


I also seem to not be able to figure out what using statement to use at the top of my file.

The nuget package is here: https://www.nuget.org/packages/csharp-extensions The method I'm trying to use from it is Object#Send

so, I call

<#objectInstanec>.Send("SomeMethod")

but the compiler says that the method is not defined on type object.
Send is defined here though: https://github.com/NullVoxPopuli/csharp-extensions/blob/master/Extensions/Methods.cs#L26

I've tried various using statements:

using csharp_extensions.Extensions.Methods;
using csharp_extensions.Extensions;
using csharp_extensions

none seem to work (csharp_extensions doesn't exist)

UPDATE - how I install the package

my project.json:

{
    "dependencies": {
        "System.Reflection": "4.1.0-beta-*",
        "Microsoft.Extensions.PlatformAbstractions": "(1.0.0-rc1-final,]",
        "xunit": "2.1.0-*",
        "xunit.runner.dnx": "2.1.0-*",
        "csharp-extensions": "1.0.1"
    },
    "commands": {
        "test": "xunit.runner.dnx"
    },
    "frameworks": {
        "dnxcore50": {
            "_": "this is the recommended windows runtime",
            "dependencies": {
                "System.Console": "4.0.0-beta-*",
                "System.Reflection": "4.1.0-beta-*",
                "System.Reflection.TypeExtensions": "4.1.0-beta-*",
                "System.Runtime.Extensions": "(4.0,]",
                "System.IO": "(4.0,]",
                "csharp-extensions": "1.0.1"

            }
        }
    }
}

and then I install the dependencies via

dnu restore


Solution

  • Your NuGet package does not look correct to me. Your assemblies are in bin\Debug\dnxcore50. I would take a look at the NuGet documentation or download an existing NuGet package that works.

    NuGet v2 style packages contain the assemblies inside lib directories. The directory inside the lib directory is named after the target framework. Taking a look at the Microsoft.Extensions.PlatformAbstractions NuGet package you have an assembly inside both the directories:

    lib\net451
    lib\dotnet5.4
    

    So with your NuGet package I believe the assemblies are not being referenced since they are not in the correct directory. The dotnet5.4 directory has the assembly that can be used with the dnxcore target framework.

    Also, the project.json should look like this (without system.reflection, as it's redundant)

    {
        "dependencies": {
            "Microsoft.Extensions.PlatformAbstractions": "(1.0.0-rc1-final,]",
            "System.Reflection": "4.1.0-beta-*",
            "xunit": "2.1.0-*",
            "xunit.runner.dnx": "2.1.0-*"
        },
        "commands": {
            "run": "csharp_extensions",
            "test": "xunit.runner.dnx"
        },
        "frameworks": {
            "dnxcore50": {
                "_": "this is the recommended windows runtime",
                "dependencies": {
                    "System.Console": "4.0.0-beta-*",
                    "System.Reflection.TypeExtensions": "4.1.0-beta-*",
                    "System.Runtime.Extensions": "(4.0,]",
                    "System.Dynamic.Runtime": "(4.0.0,]",
                    "Microsoft.CSharp": "(4.0.0,]",
                    "System.IO": "(4.0,]"
                }
            }
        }
    }