简体   繁体   English

尝试从Type中创建Roslyn中的类型,但是在转换可空时我得到BadImageFormatException

[英]Trying to create a type in Roslyn from a Type, but I get BadImageFormatException when converting a nullable

I'm trying to write a function that takes in an existing Type, and converts the Type to a PropertyDeclaration. 我正在尝试编写一个接受现有Type的函数,并将Type转换为PropertyDeclaration。 I almost have it working, but if the Type I pass in is Nullable, I get this error when I eventually try to compile my class with it: System.BadImageFormatException: is not a valid Win32 application. 我几乎让它工作,但如果Type I传入的是Nullable,当我最终尝试用它编译我的类时,我得到这个错误: System.BadImageFormatException:不是一个有效的Win32应用程序。 (Exception from HRESULT: 0x800700C1). (HRESULT异常:0x800700C1)。

Here's my code: 这是我的代码:

var classDeclaration = SyntaxFactory.ClassDeclaration("class name");
classDeclaration.AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword));
classDeclaration.AddMembers(ConvertToProperty(myType, myTypeName));

private static PropertyDeclarationSyntax ConvertToProperty(Type propertyType, string propertyName)
{
    var typeSyntax = SyntaxFactory.ParseTypeName(propertyType.ToString());
    var propertyDeclaration = SyntaxFactory.PropertyDeclaration(typeSyntax, propertyName)
        .AddModifiers(SyntaxFactory.Token(SyntaxKind.PublicKeyword))
        .AddAccessorListAccessors(
            SyntaxFactory.AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)),
            SyntaxFactory.AccessorDeclaration(SyntaxKind.SetAccessorDeclaration).WithSemicolonToken(SyntaxFactory.Token(SyntaxKind.SemicolonToken)));

    return propertyDeclaration;
}

The class will compile just fine if the Type I pass in is not nullable. 如果Type I传入不可为空,那么该类将编译得很好。 I'm not very familiar with Roslyn and I haven't found any examples online of someone doing what i'm trying to do. 我对Roslyn不是很熟悉,我没有在网上找到任何人在做我正在做的事情的例子。 Is anyone more familiar and has an idea? 有人比较熟悉并有想法吗?

First, you really need to add more details to your question. 首先,您确实需要在问题中添加更多详细信息。 Look here: Minimal, Complete, and Verifiable example 看这里: 最小,完整和可验证的例子

To your problem, when you run your code on a non nullable type, lets say int you will get this output: 对于您的问题,当您在非nullable类型上运行代码时,让我们说int将获得此输出:

public class MyClass
{
    public System.Int32 MyProperty { get; set; }
}

But with nullable , you will get this: 但是有了nullable ,你会得到这个:

public class MyClass
{
    public System.Nullable`1[System.Int32] MyProperty { get; set; }
}

You can see that it is not a valid C# code. 您可以看到它不是有效的C#代码。

To solve it, check this answer , just copy paste and the result will look like this: 要解决它,请检查此答案 ,只需复制粘贴,结果将如下所示:

public class MyClass
{
    public Nullable<Int32> MyProperty { get; set; }
}

Full code: 完整代码:

private static PropertyDeclarationSyntax ConvertToProperty(Type propertyType, string propertyName)
{
    var typeSyntax = ParseTypeName(propertyType.ToGenericTypeString());
    return PropertyDeclaration(typeSyntax, propertyName)
            .AddModifiers(Token(SyntaxKind.PublicKeyword))
            .AddAccessorListAccessors(
                AccessorDeclaration(SyntaxKind.GetAccessorDeclaration).WithSemicolonToken(Token(SyntaxKind.SemicolonToken)),
                AccessorDeclaration(SyntaxKind.SetAccessorDeclaration).WithSemicolonToken(Token(SyntaxKind.SemicolonToken)));
}

var classDeclaration = ClassDeclaration("MyClass");
classDeclaration = classDeclaration.AddModifiers(Token(SyntaxKind.PublicKeyword));
classDeclaration = classDeclaration.AddMembers(ConvertToProperty(typeof(int?), "MyProperty"));

Console.WriteLine(classDeclaration.ToString());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM