CS0246 error when using custom formatter for generic types
まだ誰も着手していません。
評価
調査の方向性
.NET 8 上で MessagePack v3.1.3 を使用し、提供された Person、Foo、FooMessagePackFormatter の定義で問題を再現してから、レポートに示されている生成された PersonFormatter.g.cs を調査します。生成された formatter が有効なジェネリック型参照を使用し、サンプルが CS0246 なしでコンパイルできれば作業完了です。
索引モデルが issue の本文から書いたものです。
説明
Bug description
CS0246 error when using generic class in custom formatter when class with MessagePackObject(true) attribute or set the Key attribute to string
CS0246 does not occur if Key attribute is set to a numeric value
Repro steps
[MessagePackObject(true)]
public record Person
{
[MessagePackFormatter(typeof(FooMessagePackFormatter<int>))]
public Foo<int> Foo { get; set; } = new Foo<int>();
}
public class Foo<T> { }
// Formatter
public class FooMessagePackFormatter<T> : IMessagePackFormatter<Foo<T>>
{
public Foo<T> Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options) => throw new NotImplementedException();
public void Serialize(ref MessagePackWriter writer, Foo<T> value, MessagePackSerializerOptions options) => throw new NotImplementedException();
}
Error
Severity Code Description Project File Line Suppression State Details
Error (active) CS0246 The type or namespace name 'T' could not be found (are you missing a using directive or an assembly reference?) ConsoleAppTest ~\ConsoleAppTest\obj\Debug\net8.0\MessagePack.SourceGenerator\MessagePack.SourceGenerator.MessagePackGenerator\Formatters.MessagePack.GeneratedMessagePackResolver.ConsoleAppTest.PersonFormatter.g.cs 12
Formatters.MessagePack.GeneratedMessagePackResolver.ConsoleAppTest.PersonFormatter.g.cs
// <auto-generated />
#pragma warning disable 618, 612, 414, 168, CS1591, SA1129, SA1309, SA1312, SA1403, SA1649
using MsgPack = global::MessagePack;
namespace MessagePack {
partial class GeneratedMessagePackResolver {
internal partial class ConsoleAppTest {
internal sealed class PersonFormatter : global::MessagePack.Formatters.IMessagePackFormatter<global::ConsoleAppTest.Person>
{
private readonly global::ConsoleAppTest.FooMessagePackFormatter<T> __FooCustomFormatter__ = new global::ConsoleAppTest.FooMessagePackFormatter<int>();
// Foo
private static global::System.ReadOnlySpan<byte> GetSpan_Foo() => new byte[1 + 3] { 163, 70, 111, 111 };
public void Serialize(ref global::MessagePack.MessagePackWriter writer, global::ConsoleAppTest.Person value, global::MessagePack.MessagePackSerializerOptions options)
{
if (value is null)
{
writer.WriteNil();
return;
}
writer.WriteMapHeader(1);
writer.WriteRaw(GetSpan_Foo());
this.__FooCustomFormatter__.Serialize(ref writer, value.Foo, options);
}
public global::ConsoleAppTest.Person Deserialize(ref global::MessagePack.MessagePackReader reader, global::MessagePack.MessagePackSerializerOptions options)
{
if (reader.TryReadNil())
{
return null;
}
options.Security.DepthStep(ref reader);
var length = reader.ReadMapHeader();
var ____result = new global::ConsoleAppTest.Person();
for (int i = 0; i < length; i++)
{
var stringKey = global::MessagePack.Internal.CodeGenHelpers.ReadStringSpan(ref reader);
switch (stringKey.Length)
{
default:
FAIL:
reader.Skip();
continue;
case 3:
if (global::MessagePack.Internal.AutomataKeyGen.GetKey(ref stringKey) != 7302982UL) { goto FAIL; }
____result.Foo = this.__FooCustomFormatter__.Deserialize(ref reader, options);
continue;
}
}
reader.Depth--;
return ____result;
}
}
}
}
}
Expected behavior
The code should compile without error CS0246.
Actual behavior
- Version used: MessagePack v3.1.3
- Runtime: .Net 8.0
- 主要言語
- C#
- スター
- 6.8k
- フォーク
- 783
- 平均マージ
- 19時間 47分
- マージ済み PR(30日)
- 3
環境構築
このプロジェクトの開発コンテナを、あなたの GitHub アカウントでブラウザ上に起動します。
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
MessagePack-CSharp/MessagePack-CSharp のほかの issue
-
SerializableMemberTypeAnalyzer throws AD0001 when any referenced assembly has an assembly-level attribute with a single array argument対応中かも このイシューにリンクされたプルリクエストがオープン中、またはマージ済みです。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
MessagePack-CSharp/MessagePack-CSharp#2324 · コメント 1 件 ·
-
難易度 1/5 1時間未満 初心者へのやさしさ 76/100
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
MessagePack-CSharp/MessagePack-CSharp#2276 · コメント 2 件 ·
-
難易度 2/5 1〜3時間 初心者へのやさしさ 66/100
MessagePack-CSharp/MessagePack-CSharp#2252 · コメント 3 件 ·
-
dynamic type fails sourcegenerator CS8785 'MessagePackGenerator' failed to generate source.対応中かも @1AbdullahButt が 13 日前に担当しました。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 25/100
MessagePack-CSharp/MessagePack-CSharp#2318 · コメント 2 件 ·
MessagePack-CSharp/MessagePack-CSharp の issue をすべて見る
似ている issue
-
type:task ux
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
elsa-workflows/elsa-foundation#5253 ·
メンテナーはふだん 1 日以内に返信