I ran into an issue when making some tests where I tried to use MarshalMode as a flags enum, which lead to the generated code not being correct, but no warning was showing up. This PR creates a warning if you try to do an operation on the MarshalMode argument or if a casted int value is not defined in the MarshalMode enum.
Fixes #87139
using System;
using System.Collections.Generic;
using System.Collections.Immutable;
-using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
-using System.Runtime.InteropServices;
-using System.Text;
using Microsoft.CodeAnalysis;
-using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Diagnostics;
using Microsoft.CodeAnalysis.DotnetRuntime.Extensions;
isEnabledByDefault: true,
description: GetResourceString(nameof(SR.ManagedTypeMustBeNonNullDescription)));
+ /// <inheritdoc cref="SR.MarshalModeMustBeValidEnumValue" />
+ public static readonly DiagnosticDescriptor MarshalModeMustBeValidValue =
+ new DiagnosticDescriptor(
+ Ids.InvalidCustomMarshallerAttributeUsage,
+ GetResourceString(nameof(SR.InvalidMarshalModeTitle)),
+ GetResourceString(nameof(SR.MarshalModeMustBeValidEnumValue)),
+ Category,
+ DiagnosticSeverity.Error,
+ isEnabledByDefault: true,
+ description: GetResourceString(nameof(SR.MarshalModeMustBeValidEnumValue)));
+
// We are intentionally using the same diagnostic IDs as the parent type.
// These diagnostics are the same diagnostics, but with a different severity,
// as the Default marshaller shape can have support for the managed-to-unmanaged shape
{
return;
}
+ var marshalModeArgument = attrCreation.GetArgumentByOrdinal(1);
+ if (marshalModeArgument.Value is not IFieldReferenceOperation { ConstantValue.Value: var marshalMode }
+ || !Enum.IsDefined(typeof(MarshalMode), (MarshalMode)marshalMode))
+ {
+ DiagnosticReporter marshalModeReporter = DiagnosticReporter.CreateForLocation(marshalModeArgument.Syntax.GetLocation(), context.ReportDiagnostic);
+ marshalModeReporter.CreateAndReportDiagnostic(MarshalModeMustBeValidValue);
+ return;
+ }
AnalyzeMarshallerType(
marshallerTypeReporter,
managedType,
- (MarshalMode)attrCreation.GetArgumentByOrdinal(1).Value.ConstantValue.Value,
+ (MarshalMode)marshalMode,
(INamedTypeSymbol)marshallerType,
ManualTypeMarshallingHelper.IsLinearCollectionEntryPoint(entryType));
}
<?xml version="1.0" encoding="utf-8"?>
<root>
- <!--
- Microsoft ResX Schema
-
+ <!--
+ Microsoft ResX Schema
+
Version 2.0
-
- The primary goals of this format is to allow a simple XML format
- that is mostly human readable. The generation and parsing of the
- various data types are done through the TypeConverter classes
+
+ The primary goals of this format is to allow a simple XML format
+ that is mostly human readable. The generation and parsing of the
+ various data types are done through the TypeConverter classes
associated with the data types.
-
+
Example:
-
+
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
-
- There are any number of "resheader" rows that contain simple
+
+ There are any number of "resheader" rows that contain simple
name/value pairs.
-
- Each data row contains a name, and value. The row also contains a
- type or mimetype. Type corresponds to a .NET class that support
- text/value conversion through the TypeConverter architecture.
- Classes that don't support this are serialized and stored with the
+
+ Each data row contains a name, and value. The row also contains a
+ type or mimetype. Type corresponds to a .NET class that support
+ text/value conversion through the TypeConverter architecture.
+ Classes that don't support this are serialized and stored with the
mimetype set.
-
- The mimetype is used for serialized objects, and tells the
- ResXResourceReader how to depersist the object. This is currently not
+
+ The mimetype is used for serialized objects, and tells the
+ ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
-
- Note - application/x-microsoft.net.object.binary.base64 is the format
- that the ResXResourceWriter will generate, however the reader can
+
+ Note - application/x-microsoft.net.object.binary.base64 is the format
+ that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
-
+
mimetype: application/x-microsoft.net.object.binary.base64
- value : The object must be serialized with
+ value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
-
+
mimetype: application/x-microsoft.net.object.soap.base64
- value : The object must be serialized with
+ value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
- value : The object must be serialized into a byte array
+ value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<data name="ConvertToLibraryImportWithSuffixAddUnsafe" xml:space="preserve">
<value>Convert to 'LibraryImport' with '{0}' suffix and enable unsafe code</value>
</data>
+ <data name="InvalidMarshalModeTitle" xml:space="preserve">
+ <value>Invalid 'MarshalMode' value.</value>
+ </data>
+ <data name="MarshalModeMustBeValidEnumValue" xml:space="preserve">
+ <value>The 'marshalMode' argument of 'CustomMarshallerAttribute' must be a valid enum value of 'MarshalMode'.</value>
+ </data>
</root>
<target state="translated">Zadaný spravovaný typ je neplatný</target>
<note />
</trans-unit>
+ <trans-unit id="InvalidMarshalModeTitle">
+ <source>Invalid 'MarshalMode' value.</source>
+ <target state="new">Invalid 'MarshalMode' value.</target>
+ <note />
+ </trans-unit>
<trans-unit id="InvalidMarshallerTypeTitle">
<source>Specified marshaller type is invalid</source>
<target state="translated">Zadaný zařazovací typ je neplatný</target>
<target state="translated">Spravovaný typ pro zařazovací typ vstupního bodu {0} nesmí nabývat hodnoty null.</target>
<note />
</trans-unit>
+ <trans-unit id="MarshalModeMustBeValidEnumValue">
+ <source>The 'marshalMode' argument of 'CustomMarshallerAttribute' must be a valid enum value of 'MarshalMode'.</source>
+ <target state="new">The 'marshalMode' argument of 'CustomMarshallerAttribute' must be a valid enum value of 'MarshalMode'.</target>
+ <note />
+ </trans-unit>
<trans-unit id="MarshallerTypeMustBeClosedOrMatchArityDescription">
<source>The marshaller type must be a closed generic or have the same number of generic parameters as the managed type so the emitted code can use a specific instantiation.</source>
<target state="translated">Zařazovací typ musí být uzavřený obecný typ nebo mít stejný počet obecných parametrů jako spravovaný typ, aby mohl vygenerovaný kód použít konkrétní vytvoření instance.</target>
<target state="translated">Der angegebene verwaltete Typ ist ungültig.</target>
<note />
</trans-unit>
+ <trans-unit id="InvalidMarshalModeTitle">
+ <source>Invalid 'MarshalMode' value.</source>
+ <target state="new">Invalid 'MarshalMode' value.</target>
+ <note />
+ </trans-unit>
<trans-unit id="InvalidMarshallerTypeTitle">
<source>Specified marshaller type is invalid</source>
<target state="translated">Der angegebene Marshallertyp ist ungültig.</target>
<target state="translated">Der verwaltete Typ für den Einstiegspunkt-Marshallertyp "{0}" darf nicht "NULL" sein</target>
<note />
</trans-unit>
+ <trans-unit id="MarshalModeMustBeValidEnumValue">
+ <source>The 'marshalMode' argument of 'CustomMarshallerAttribute' must be a valid enum value of 'MarshalMode'.</source>
+ <target state="new">The 'marshalMode' argument of 'CustomMarshallerAttribute' must be a valid enum value of 'MarshalMode'.</target>
+ <note />
+ </trans-unit>
<trans-unit id="MarshallerTypeMustBeClosedOrMatchArityDescription">
<source>The marshaller type must be a closed generic or have the same number of generic parameters as the managed type so the emitted code can use a specific instantiation.</source>
<target state="translated">Der Marshaller-Typ muss ein geschlossener generischer Typ sein oder dieselbe Anzahl generischer Parameter wie der verwaltete Typ aufweisen, damit der ausgegebene Code eine bestimmte Instanziierung verwenden kann.</target>
<target state="translated">El tipo administrado especificado no es válido</target>
<note />
</trans-unit>
+ <trans-unit id="InvalidMarshalModeTitle">
+ <source>Invalid 'MarshalMode' value.</source>
+ <target state="new">Invalid 'MarshalMode' value.</target>
+ <note />
+ </trans-unit>
<trans-unit id="InvalidMarshallerTypeTitle">
<source>Specified marshaller type is invalid</source>
<target state="translated">El tipo de serializador especificado no es válido</target>
<target state="translated">El tipo administrado para el tipo de serializador de punto de entrada "{0}" no debe ser "null"</target>
<note />
</trans-unit>
+ <trans-unit id="MarshalModeMustBeValidEnumValue">
+ <source>The 'marshalMode' argument of 'CustomMarshallerAttribute' must be a valid enum value of 'MarshalMode'.</source>
+ <target state="new">The 'marshalMode' argument of 'CustomMarshallerAttribute' must be a valid enum value of 'MarshalMode'.</target>
+ <note />
+ </trans-unit>
<trans-unit id="MarshallerTypeMustBeClosedOrMatchArityDescription">
<source>The marshaller type must be a closed generic or have the same number of generic parameters as the managed type so the emitted code can use a specific instantiation.</source>
<target state="translated">El tipo serializador debe ser un genérico cerrado o tener el mismo número de parámetros genéricos que el tipo administrado para que el código emitido pueda usar una creación de instancia específica.</target>
<target state="translated">Le type managé spécifié n’est pas valide</target>
<note />
</trans-unit>
+ <trans-unit id="InvalidMarshalModeTitle">
+ <source>Invalid 'MarshalMode' value.</source>
+ <target state="new">Invalid 'MarshalMode' value.</target>
+ <note />
+ </trans-unit>
<trans-unit id="InvalidMarshallerTypeTitle">
<source>Specified marshaller type is invalid</source>
<target state="translated">Le type de marshaleur spécifié n’est pas valide</target>
<target state="translated">Le type managé du type marshaleur de point d’entrée « {0} » ne doit pas avoir la valeur 'null'</target>
<note />
</trans-unit>
+ <trans-unit id="MarshalModeMustBeValidEnumValue">
+ <source>The 'marshalMode' argument of 'CustomMarshallerAttribute' must be a valid enum value of 'MarshalMode'.</source>
+ <target state="new">The 'marshalMode' argument of 'CustomMarshallerAttribute' must be a valid enum value of 'MarshalMode'.</target>
+ <note />
+ </trans-unit>
<trans-unit id="MarshallerTypeMustBeClosedOrMatchArityDescription">
<source>The marshaller type must be a closed generic or have the same number of generic parameters as the managed type so the emitted code can use a specific instantiation.</source>
<target state="translated">Le type marshaler doit être un générique fermé ou avoir le même nombre de paramètres génériques que le type managé pour que le code émis puisse utiliser une instanciation spécifique.</target>
<target state="translated">Il tipo gestito specificato non è valido</target>
<note />
</trans-unit>
+ <trans-unit id="InvalidMarshalModeTitle">
+ <source>Invalid 'MarshalMode' value.</source>
+ <target state="new">Invalid 'MarshalMode' value.</target>
+ <note />
+ </trans-unit>
<trans-unit id="InvalidMarshallerTypeTitle">
<source>Specified marshaller type is invalid</source>
<target state="translated">Il tipo di marshaller specificato non è valido</target>
<target state="translated">Il tipo gestito per il tipo di marshaller del punto di ingresso '{0}' non deve essere 'null'</target>
<note />
</trans-unit>
+ <trans-unit id="MarshalModeMustBeValidEnumValue">
+ <source>The 'marshalMode' argument of 'CustomMarshallerAttribute' must be a valid enum value of 'MarshalMode'.</source>
+ <target state="new">The 'marshalMode' argument of 'CustomMarshallerAttribute' must be a valid enum value of 'MarshalMode'.</target>
+ <note />
+ </trans-unit>
<trans-unit id="MarshallerTypeMustBeClosedOrMatchArityDescription">
<source>The marshaller type must be a closed generic or have the same number of generic parameters as the managed type so the emitted code can use a specific instantiation.</source>
<target state="translated">Il tipo di marshaller deve essere un generico chiuso o avere lo stesso numero di parametri generici del tipo gestito, in modo che il codice generato possa usare una creazione di istanza specifica.</target>
<target state="translated">指定されたマネージド型が無効です</target>
<note />
</trans-unit>
+ <trans-unit id="InvalidMarshalModeTitle">
+ <source>Invalid 'MarshalMode' value.</source>
+ <target state="new">Invalid 'MarshalMode' value.</target>
+ <note />
+ </trans-unit>
<trans-unit id="InvalidMarshallerTypeTitle">
<source>Specified marshaller type is invalid</source>
<target state="translated">指定されたマーシャラー型が無効です</target>
<target state="translated">エントリ ポイント マーシャラー型 '{0}' のマネージド型を 'null' にすることはできません</target>
<note />
</trans-unit>
+ <trans-unit id="MarshalModeMustBeValidEnumValue">
+ <source>The 'marshalMode' argument of 'CustomMarshallerAttribute' must be a valid enum value of 'MarshalMode'.</source>
+ <target state="new">The 'marshalMode' argument of 'CustomMarshallerAttribute' must be a valid enum value of 'MarshalMode'.</target>
+ <note />
+ </trans-unit>
<trans-unit id="MarshallerTypeMustBeClosedOrMatchArityDescription">
<source>The marshaller type must be a closed generic or have the same number of generic parameters as the managed type so the emitted code can use a specific instantiation.</source>
<target state="translated">マーシャラー型は、クローズ ジェネリックであるか、マネージド型と同じ数のジェネリック パラメーターを持つ必要があります。これにより、生成されたコードが特定のインスタンス化を使用できるようになります。</target>
<target state="translated">지정된 관리 유형이 잘못되었습니다.</target>
<note />
</trans-unit>
+ <trans-unit id="InvalidMarshalModeTitle">
+ <source>Invalid 'MarshalMode' value.</source>
+ <target state="new">Invalid 'MarshalMode' value.</target>
+ <note />
+ </trans-unit>
<trans-unit id="InvalidMarshallerTypeTitle">
<source>Specified marshaller type is invalid</source>
<target state="translated">지정된 마샬러 유형이 잘못되었습니다.</target>
<target state="translated">진입점 마샬러 유형 '{0}'의 관리 유형은 'null'이 아니어야 합니다.</target>
<note />
</trans-unit>
+ <trans-unit id="MarshalModeMustBeValidEnumValue">
+ <source>The 'marshalMode' argument of 'CustomMarshallerAttribute' must be a valid enum value of 'MarshalMode'.</source>
+ <target state="new">The 'marshalMode' argument of 'CustomMarshallerAttribute' must be a valid enum value of 'MarshalMode'.</target>
+ <note />
+ </trans-unit>
<trans-unit id="MarshallerTypeMustBeClosedOrMatchArityDescription">
<source>The marshaller type must be a closed generic or have the same number of generic parameters as the managed type so the emitted code can use a specific instantiation.</source>
<target state="translated">마샬러 형식은 닫힌 제네릭이거나 관리되는 형식과 동일한 수의 제네릭 매개 변수가 있어야 내보낸 코드에서 특정 인스턴스화를 사용할 수 있습니다.</target>
<target state="translated">Określony typ zarządzany jest nieprawidłowy</target>
<note />
</trans-unit>
+ <trans-unit id="InvalidMarshalModeTitle">
+ <source>Invalid 'MarshalMode' value.</source>
+ <target state="new">Invalid 'MarshalMode' value.</target>
+ <note />
+ </trans-unit>
<trans-unit id="InvalidMarshallerTypeTitle">
<source>Specified marshaller type is invalid</source>
<target state="translated">Określony typ marshallera jest nieprawidłowy</target>
<target state="translated">Typ zarządzany dla typu marshaller punktu wejścia „{0}” nie może mieć wartości „null”</target>
<note />
</trans-unit>
+ <trans-unit id="MarshalModeMustBeValidEnumValue">
+ <source>The 'marshalMode' argument of 'CustomMarshallerAttribute' must be a valid enum value of 'MarshalMode'.</source>
+ <target state="new">The 'marshalMode' argument of 'CustomMarshallerAttribute' must be a valid enum value of 'MarshalMode'.</target>
+ <note />
+ </trans-unit>
<trans-unit id="MarshallerTypeMustBeClosedOrMatchArityDescription">
<source>The marshaller type must be a closed generic or have the same number of generic parameters as the managed type so the emitted code can use a specific instantiation.</source>
<target state="translated">Typ marshallera musi być zamkniętym typem ogólnym lub mieć taką samą liczbę parametrów ogólnych jak typ zarządzany, aby emitowany kod mógł używać określonego wystąpienia.</target>
<target state="translated">O tipo gerenciado especificado é inválido</target>
<note />
</trans-unit>
+ <trans-unit id="InvalidMarshalModeTitle">
+ <source>Invalid 'MarshalMode' value.</source>
+ <target state="new">Invalid 'MarshalMode' value.</target>
+ <note />
+ </trans-unit>
<trans-unit id="InvalidMarshallerTypeTitle">
<source>Specified marshaller type is invalid</source>
<target state="translated">O tipo de empacotador especificado é inválido</target>
<target state="translated">O tipo gerenciado para o tipo de empacotador de ponto de entrada '{0}' não deve ser 'nulo'</target>
<note />
</trans-unit>
+ <trans-unit id="MarshalModeMustBeValidEnumValue">
+ <source>The 'marshalMode' argument of 'CustomMarshallerAttribute' must be a valid enum value of 'MarshalMode'.</source>
+ <target state="new">The 'marshalMode' argument of 'CustomMarshallerAttribute' must be a valid enum value of 'MarshalMode'.</target>
+ <note />
+ </trans-unit>
<trans-unit id="MarshallerTypeMustBeClosedOrMatchArityDescription">
<source>The marshaller type must be a closed generic or have the same number of generic parameters as the managed type so the emitted code can use a specific instantiation.</source>
<target state="translated">O tipo de empacotador deve ser um genérico fechado ou ter o mesmo número de parâmetros genéricos que o tipo gerenciado para que o código emitido possa usar uma instanciação específica.</target>
<target state="translated">Указан недопустимый управляемый тип</target>
<note />
</trans-unit>
+ <trans-unit id="InvalidMarshalModeTitle">
+ <source>Invalid 'MarshalMode' value.</source>
+ <target state="new">Invalid 'MarshalMode' value.</target>
+ <note />
+ </trans-unit>
<trans-unit id="InvalidMarshallerTypeTitle">
<source>Specified marshaller type is invalid</source>
<target state="translated">Указан недопустимый тип маршалера</target>
<target state="translated">Управляемый тип для типа маршалера точки входа "{0}" должен отличаться от "NULL"</target>
<note />
</trans-unit>
+ <trans-unit id="MarshalModeMustBeValidEnumValue">
+ <source>The 'marshalMode' argument of 'CustomMarshallerAttribute' must be a valid enum value of 'MarshalMode'.</source>
+ <target state="new">The 'marshalMode' argument of 'CustomMarshallerAttribute' must be a valid enum value of 'MarshalMode'.</target>
+ <note />
+ </trans-unit>
<trans-unit id="MarshallerTypeMustBeClosedOrMatchArityDescription">
<source>The marshaller type must be a closed generic or have the same number of generic parameters as the managed type so the emitted code can use a specific instantiation.</source>
<target state="translated">Тип маршалера должен быть закрытым универсальным или иметь то же количество универсальных параметров, что и управляемый тип, чтобы создаваемый код мог использовать конкретный экземпляр.</target>
<target state="translated">Belirtilen yönetilen tür geçersiz</target>
<note />
</trans-unit>
+ <trans-unit id="InvalidMarshalModeTitle">
+ <source>Invalid 'MarshalMode' value.</source>
+ <target state="new">Invalid 'MarshalMode' value.</target>
+ <note />
+ </trans-unit>
<trans-unit id="InvalidMarshallerTypeTitle">
<source>Specified marshaller type is invalid</source>
<target state="translated">Belirtilen hazırlayıcı türü geçersiz</target>
<target state="translated">Giriş noktası hazırlayıcı türü '{0}' için yönetilen tür 'null' olmamalıdır</target>
<note />
</trans-unit>
+ <trans-unit id="MarshalModeMustBeValidEnumValue">
+ <source>The 'marshalMode' argument of 'CustomMarshallerAttribute' must be a valid enum value of 'MarshalMode'.</source>
+ <target state="new">The 'marshalMode' argument of 'CustomMarshallerAttribute' must be a valid enum value of 'MarshalMode'.</target>
+ <note />
+ </trans-unit>
<trans-unit id="MarshallerTypeMustBeClosedOrMatchArityDescription">
<source>The marshaller type must be a closed generic or have the same number of generic parameters as the managed type so the emitted code can use a specific instantiation.</source>
<target state="translated">Hazırlayıcı türü kapalı bir genel tür olmalıdır veya gösterilen kodun belirli bir örnek oluşturma kullanabilmesi için yönetilen türle aynı sayıda genel parametreye sahip olmalıdır.</target>
<target state="translated">指定的托管类型无效</target>
<note />
</trans-unit>
+ <trans-unit id="InvalidMarshalModeTitle">
+ <source>Invalid 'MarshalMode' value.</source>
+ <target state="new">Invalid 'MarshalMode' value.</target>
+ <note />
+ </trans-unit>
<trans-unit id="InvalidMarshallerTypeTitle">
<source>Specified marshaller type is invalid</source>
<target state="translated">指定的封送处理程序类型无效</target>
<target state="translated">入口点封送处理程序类型“{0}”的托管类型不能为“null”</target>
<note />
</trans-unit>
+ <trans-unit id="MarshalModeMustBeValidEnumValue">
+ <source>The 'marshalMode' argument of 'CustomMarshallerAttribute' must be a valid enum value of 'MarshalMode'.</source>
+ <target state="new">The 'marshalMode' argument of 'CustomMarshallerAttribute' must be a valid enum value of 'MarshalMode'.</target>
+ <note />
+ </trans-unit>
<trans-unit id="MarshallerTypeMustBeClosedOrMatchArityDescription">
<source>The marshaller type must be a closed generic or have the same number of generic parameters as the managed type so the emitted code can use a specific instantiation.</source>
<target state="translated">封送处理程序类型必须是封闭泛型或具有与托管类型相同数目的泛型参数,以便发出的代码可以使用特定实例化。</target>
<target state="translated">指定的受控類型無效</target>
<note />
</trans-unit>
+ <trans-unit id="InvalidMarshalModeTitle">
+ <source>Invalid 'MarshalMode' value.</source>
+ <target state="new">Invalid 'MarshalMode' value.</target>
+ <note />
+ </trans-unit>
<trans-unit id="InvalidMarshallerTypeTitle">
<source>Specified marshaller type is invalid</source>
<target state="translated">指定的封送處理器類型無效</target>
<target state="translated">進入點封送處理器類型 '{0}' 的受控類型不得為 'null'</target>
<note />
</trans-unit>
+ <trans-unit id="MarshalModeMustBeValidEnumValue">
+ <source>The 'marshalMode' argument of 'CustomMarshallerAttribute' must be a valid enum value of 'MarshalMode'.</source>
+ <target state="new">The 'marshalMode' argument of 'CustomMarshallerAttribute' must be a valid enum value of 'MarshalMode'.</target>
+ <note />
+ </trans-unit>
<trans-unit id="MarshallerTypeMustBeClosedOrMatchArityDescription">
<source>The marshaller type must be a closed generic or have the same number of generic parameters as the managed type so the emitted code can use a specific instantiation.</source>
<target state="translated">封送處理器類型必須是封閉式泛型或具有與受控類型相同的泛型參數數目,因此發出的程式碼可以使用特定的具現化。</target>
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.Testing;
using System.Collections.Generic;
+using System.Runtime.InteropServices.Marshalling;
using System.Threading.Tasks;
using Xunit;
using static Microsoft.Interop.Analyzers.CustomMarshallerAttributeAnalyzer;
await VerifyCS.VerifyAnalyzerAsync(source,
VerifyCS.Diagnostic(ManagedTypeMustBeNonNullRule).WithLocation(0).WithArguments("MarshallerType"));
}
+ [Fact]
+ public async Task UsingMarshalModeAsFlags_ReportsDiagnostic()
+ {
+ string source = """
+ using System.Collections.Generic;
+ using System.Runtime.InteropServices;
+ using System.Runtime.InteropServices.Marshalling;
+ [CustomMarshaller(typeof(int), {|#0:MarshalMode.ElementIn | MarshalMode.ElementOut | MarshalMode.Default|}, typeof(MyMarshaller))]
+ public static class MyMarshaller
+ {
+
+ }
+ """;
+
+ await VerifyCS.VerifyAnalyzerAsync(source,
+ VerifyCS.Diagnostic(MarshalModeMustBeValidValue).WithLocation(0));
+ }
+
+ [Fact]
+ public async Task UsingInvalidMarshalMode_ReportsDiagnostic()
+ {
+ string source = """
+ using System.Collections.Generic;
+ using System.Runtime.InteropServices;
+ using System.Runtime.InteropServices.Marshalling;
+ [CustomMarshaller(typeof(int), {|#0:(MarshalMode)10|}, typeof(MyMarshaller))]
+ public static class MyMarshaller
+ {
+
+ }
+ """;
+
+ await VerifyCS.VerifyAnalyzerAsync(source,
+ VerifyCS.Diagnostic(MarshalModeMustBeValidValue).WithLocation(0));
+ }
[Fact]
public async Task MarshallerWithEntryPointAttributeForType_DoesNotReportDiagnostic()