1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

既存のアセンブリのシンプルなクラスをRoslynを使用してコードに戻す方法

Last updated at Posted at 2023-02-24

既存のプログラムアセンブリからエンティティコードのコピーを作成する必要がある場合がありますが、これらのコードを手動でコピーするのは時間がかかる場合があります。このプロセスを簡素化するために、Roslynを使用してコードを自動生成できます。

この記事では、Roslynを使用してシンプルなクラスをコードに戻す方法について説明します。

実現方法

この機能を実現するには、次の手順が必要です。

  1. クラス内のすべてのプロパティ型がサポートされている型リストに含まれているかをチェックします。

  2. C#コード生成ユニットを作成し、必要な名前空間を追加します。

  3. 名前空間を作成し、クラス定義を追加します。

  4. クラスのすべてのプロパティをクラス定義に追加します。

  5. 各プロパティにget/setメソッドを追加し、default値を設定します。

コードの実装

サポートされる型をチェックします

PropertyInfo配列から、サポートされるプロパティ型かどうかをチェックする静的メソッドです。まず、サポートされるプロパティ型のリストを定義し、その中にGUIDDateTimestringintなどが含まれます。

次に、プロパティ情報の配列を順番にチェックして、各プロパティの型が配列の場合は、配列の要素型を取得します。その後、サポートされるプロパティ型のリストに含まれている場合は、次のプロパティに進みます。サポートされていないプロパティ型が見つかった場合は、NotSupportedExceptionをスローします。

private static void CheckSamplePropertiesTypes(PropertyInfo[] propertyInfos)
{
    var supportTypes = new[]
    {
        typeof(Guid),
        typeof(DateTime),
        typeof(string),

        typeof(int),
        typeof(char),
        typeof(uint),
        typeof(short),
        typeof(ushort),
        typeof(long),
        typeof(ulong),
        typeof(bool),
        typeof(float),
        typeof(double),
        typeof(byte)
    };

    foreach (var p in propertyInfos)
    {
        var propertyType = p.PropertyType;
        if (propertyType.BaseType == typeof(Array))
        {
            propertyType = propertyType.GetElementType();
        }

        if (supportTypes.Contains(propertyType))
        {
            continue;
        }

        throw new NotSupportedException();
    }
}

プロパティを追加します

プロパティの名前と型から、プロパティ定義を作成します。その後、プロパティのget/setアクセサを設定し、AddPropertyInitValueメソッドを使用してプロパティの初期値を設定します。最後に、AddMembersメソッドを使用して、クラス定義に新しいプロパティを追加します。

AddPropertyInitValueメソッドは、プロパティの初期値を設定するために使用されます。まず、プロパティの型に応じて、初期値を設定するためのコードを生成します。例えば、プロパティの型がstringの場合は、初期値に空文字列を設定します。また、プロパティの型が配列の場合は、要素の型から配列を生成し、初期値に設定します。

private static ClassDeclarationSyntax AddProperty(ClassDeclarationSyntax classDeclaration, PropertyInfo propertyInfo)
{
    var propertyDeclaration = SyntaxFactory.PropertyDeclaration(
            SyntaxFactory.ParseTypeName(propertyInfo.PropertyType.Name),
            propertyInfo.Name)
        .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
        .AddAccessorListAccessors(
            SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
                .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)),
            SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration)
                .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)));


    propertyDeclaration = AddPropertyInitValue(propertyDeclaration, propertyInfo);

    return classDeclaration.AddMembers(propertyDeclaration);
}

private static PropertyDeclarationSyntax AddPropertyInitValue(
    PropertyDeclarationSyntax propertyDeclaration,
    PropertyInfo propertyInfo)
{
    var propertyType = propertyInfo.PropertyType;
    var newLine = Environment.NewLine;

    if (propertyType == typeof(string))
    {
        propertyDeclaration = propertyDeclaration.WithInitializer(
                SyntaxFactory.EqualsValueClause(
                    SyntaxFactory.LiteralExpression(
                        SyntaxKind.StringLiteralExpression,
                        SyntaxFactory.Literal(""))))
            .WithSemicolonToken(
                SyntaxFactory.Token(SyntaxKind.SemicolonToken))
            .WithTrailingTrivia(SyntaxFactory.Comment(newLine));
    }
    else if (propertyType.BaseType == typeof(Array))
    {
        var elementType = propertyType.GetElementType()!;

        propertyDeclaration = propertyDeclaration.WithInitializer(
                SyntaxFactory.EqualsValueClause(
                    SyntaxFactory.ArrayCreationExpression(
                        SyntaxFactory
                            .ArrayType(SyntaxFactory.ParseTypeName(elementType.Name))
                            .WithRankSpecifiers(
                                SyntaxFactory.SingletonList(
                                    SyntaxFactory.ArrayRankSpecifier(
                                        SyntaxFactory.SingletonSeparatedList<ExpressionSyntax>(
                                            SyntaxFactory.LiteralExpression(
                                                SyntaxKind.NumericLiteralExpression,
                                                SyntaxFactory.Literal(0)))))))))
            .WithSemicolonToken(
                SyntaxFactory.Token(SyntaxKind.SemicolonToken))
            .WithTrailingTrivia(SyntaxFactory.Comment(newLine));
    }

    return propertyDeclaration;
}

クラスと名前空間を追加して組み立てます

この関数は、与えられたType型のクラスの全てのプロパティが支援される型であることを確認し、C#構文ツリーを生成して返します。生成されたC#コードはstringとして返されます。

public static string GenerateSampleClass(
    Type type,
    string newClassNamespace,
    Func<Type, PropertyInfo[]>? getPropertyInfosFunc = null)
{
    getPropertyInfosFunc ??= t => t.GetProperties();

    var allProperties = getPropertyInfosFunc.Invoke(type);
    CheckSamplePropertiesTypes(allProperties);

    var syntax = SyntaxFactory.CompilationUnit()
        .AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("System")))
        .WithLeadingTrivia(SyntaxFactory.Comment("// ReSharper disable All"));

    var @namespace = SyntaxFactory.NamespaceDeclaration(SyntaxFactory.ParseName(newClassNamespace))
        .NormalizeWhitespace();
    var classDeclaration = SyntaxFactory.ClassDeclaration(type.Name)
        .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword));


    foreach (var p in allProperties)
    {
        classDeclaration = AddProperty(classDeclaration, p);
    }

    @namespace = @namespace.AddMembers(classDeclaration);
    syntax = syntax.AddMembers(@namespace);

    return syntax.NormalizeWhitespace().ToFullString();
}

全部のコード

  • CodeGenerateTools.cs
public static class CodeGenerateTools
{
    private static void CheckSamplePropertiesTypes(PropertyInfo[] propertyInfos)
    {
        var supportTypes = new[]
        {
            typeof(Guid),
            typeof(DateTime),
            typeof(string),

            typeof(int),
            typeof(char),
            typeof(uint),
            typeof(short),
            typeof(ushort),
            typeof(long),
            typeof(ulong),
            typeof(bool),
            typeof(float),
            typeof(double),
            typeof(byte)
        };

        foreach (var p in propertyInfos)
        {
            var propertyType = p.PropertyType;
            if (propertyType.BaseType == typeof(Array))
            {
                propertyType = propertyType.GetElementType();
            }

            if (supportTypes.Contains(propertyType))
            {
                continue;
            }

            throw new NotSupportedException();
        }
    }

    public static string GenerateSampleClass(
        Type type,
        string newClassNamespace,
        Func<Type, PropertyInfo[]>? getPropertyInfosFunc = null)
    {
        getPropertyInfosFunc ??= t => t.GetProperties();

        var allProperties = getPropertyInfosFunc.Invoke(type);
        CheckSamplePropertiesTypes(allProperties);

        var syntax = SyntaxFactory.CompilationUnit()
            .AddUsings(SyntaxFactory.UsingDirective(SyntaxFactory.ParseName("System")))
            .WithLeadingTrivia(SyntaxFactory.Comment("// ReSharper disable All"));

        var @namespace = SyntaxFactory.NamespaceDeclaration(SyntaxFactory.ParseName(newClassNamespace))
            .NormalizeWhitespace();
        var classDeclaration = SyntaxFactory.ClassDeclaration(type.Name)
            .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword));


        foreach (var p in allProperties)
        {
            classDeclaration = AddProperty(classDeclaration, p);
        }

        @namespace = @namespace.AddMembers(classDeclaration);
        syntax = syntax.AddMembers(@namespace);

        return syntax.NormalizeWhitespace().ToFullString();
    }

    private static ClassDeclarationSyntax AddProperty(ClassDeclarationSyntax classDeclaration, PropertyInfo propertyInfo)
    {
        var propertyDeclaration = SyntaxFactory.PropertyDeclaration(
                SyntaxFactory.ParseTypeName(propertyInfo.PropertyType.Name),
                propertyInfo.Name)
            .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
            .AddAccessorListAccessors(
                SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration)
                    .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)),
                SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration)
                    .WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)));


        propertyDeclaration = AddPropertyInitValue(propertyDeclaration, propertyInfo);

        return classDeclaration.AddMembers(propertyDeclaration);
    }


    private static PropertyDeclarationSyntax AddPropertyInitValue(
        PropertyDeclarationSyntax propertyDeclaration,
        PropertyInfo propertyInfo)
    {
        var propertyType = propertyInfo.PropertyType;
        var newLine = Environment.NewLine;

        if (propertyType == typeof(string))
        {
            propertyDeclaration = propertyDeclaration.WithInitializer(
                    SyntaxFactory.EqualsValueClause(
                        SyntaxFactory.LiteralExpression(
                            SyntaxKind.StringLiteralExpression,
                            SyntaxFactory.Literal(""))))
                .WithSemicolonToken(
                    SyntaxFactory.Token(SyntaxKind.SemicolonToken))
                .WithTrailingTrivia(SyntaxFactory.Comment(newLine));
        }
        else if (propertyType.BaseType == typeof(Array))
        {
            var elementType = propertyType.GetElementType()!;

            propertyDeclaration = propertyDeclaration.WithInitializer(
                    SyntaxFactory.EqualsValueClause(
                        SyntaxFactory.ArrayCreationExpression(
                            SyntaxFactory
                                .ArrayType(SyntaxFactory.ParseTypeName(elementType.Name))
                                .WithRankSpecifiers(
                                    SyntaxFactory.SingletonList(
                                        SyntaxFactory.ArrayRankSpecifier(
                                            SyntaxFactory.SingletonSeparatedList<ExpressionSyntax>(
                                                SyntaxFactory.LiteralExpression(
                                                    SyntaxKind.NumericLiteralExpression,
                                                    SyntaxFactory.Literal(0)))))))))
                .WithSemicolonToken(
                    SyntaxFactory.Token(SyntaxKind.SemicolonToken))
                .WithTrailingTrivia(SyntaxFactory.Comment(newLine));
        }

        return propertyDeclaration;
    }
}
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?