jburzynski / TypeGen

Single-class-per-file C# to TypeScript generator
MIT License
194 stars 55 forks source link

Programmatical API: Could not load file or assembly 'Namotion.Reflection #188

Open jonashw opened 7 months ago

jonashw commented 7 months ago

First of all, what an exciting library! I'm eager to apply it to my project.

I've been unable to overcome the following exception with a bare-minimum usage of the programmatical API and .NET 7.

Could not load file or assembly 'Namotion.Reflection, Version=2.1.1.0, Culture=neutral, PublicKeyToken=c2f9c3bdfae56102'. The system cannot find the file specified. at TypeGen.Core.Extensions.TypeExtensions.IsNullable(MemberInfo memberInfo) at TypeGen.Core.Generator.Services.TypeService.GetTypeUnions(MemberInfo memberInfo) at TypeGen.Core.Generator.Generator.GetInterfacePropertyText(Type type, MemberInfo memberInfo) at TypeGen.Core.Generator.Generator.<>cDisplayClass51_0.b0(String current, MemberInfo memberInfo) at System.Linq.Enumerable.Aggregate[TSource,TAccumulate](IEnumerable1 source, TAccumulate seed, Func3 func) at TypeGen.Core.Generator.Generator.GetInterfacePropertiesText(Type type) at TypeGen.Core.Generator.Generator.GenerateInterface(Type type, ExportTsInterfaceAttribute interfaceAttribute) at TypeGen.Core.Generator.Generator.GenerateType(Type type) at TypeGen.Core.Generator.Generator.<>cDisplayClass33_0.b0() at TypeGen.Core.Generator.Generator.ExecuteWithTypeContextLogging(Action action) at TypeGen.Core.Generator.Generator.GenerateMarkedType(Type type) at TypeGen.Core.Generator.Generator.Generate(IEnumerable`1 generationSpecs) at TypeScriptGeneratorSample.Program.Main(String[] args) in C:\Users\jonashw\GitRepos\TypeScriptGeneratorSample\Program.cs:line 12

tgconfig.json:

{
  "outputPath": "generated",
  "generationSpecs": [ "SampleGenerationSpec" ]
}

C# source:

using TypeGen.Core.Generator;
using TypeGen.Core.SpecGeneration;

namespace TypeScriptGeneratorSample
{
    public class Program
    {
        public static void Main(string[] args)
        {
            Console.WriteLine("Hello, World!");
            var gen = new Generator();
            gen.Generate(new[] { new SampleGenerationSpec() });
        }
    }

    public class SampleGenerationSpec : GenerationSpec
    {
        public override void OnBeforeGeneration(OnBeforeGenerationArgs args)
        {
            AddInterface(typeof(SampleRecord));
        }
    }

    public record SampleRecord(int Id, string Name, DateTime Created, bool Active);
}

Curiously, I've been able to make SampleGenerationSpec work via dotnet-typegen generate without an issue.

generated/sample-record.ts:

/**
 * This is a TypeGen auto-generated file.
 * Any changes made to this file can be lost when this file is regenerated.
 */

export interface SampleRecord {
    id: number;
    name: string;
    created: Date;
    active: boolean;
}

Am I doing something wrong or have I discovered a bug?

millokeller commented 2 weeks ago

I'm encountering the same issue with .NET 6.0 and TypeGen 5.0.1. The only solution I've found is to manually add a package reference to Namotion.Reflection: <PackageReference Include="Namotion.Reflection" Version="2.1.1" />

Would it be possible to include Namotion.Reflection as a dependency in the TypeGen NuGet package to streamline the setup process and prevent this issue?