简体   繁体   English

如何使用C#构造对象-CodeDOM

[英]How to construct object using C# - CodeDOM

I have method that assign value to property and generate code statement using C# CodeDOM. 我有将值分配给属性并使用C#CodeDOM生成代码语句的方法。

private static CodeAssignStatement setProp(string propName, object propValue, Type propType, Type objType)
{
                CodeAssignStatement declareVariableName = null;
                if (propType.IsPrimitive)
                {
                    declareVariableName = new CodeAssignStatement(
                   new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("testObj"), propName), new CodePrimitiveExpression(propValue)
                   );
                }
                else
                {
                    declareVariableName = new CodeAssignStatement(
                    new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("testObj"), propName),
                     new CodeVariableReferenceExpression("\"" + propValue?.ToString() + "\"")
                    );
                }

                return declareVariableName;
}

For primitive value it is generating statements correctly. 对于原始值,它可以正确生成语句。 However, for rest, eg DateTime it generates statement like testObj.PurchasedOn = "17-09-2016 18:50:00"; 但是,对于其余时间(例如DateTime),它会生成类似testObj.PurchasedOn = "17-09-2016 18:50:00";语句testObj.PurchasedOn = "17-09-2016 18:50:00"; . One way to use target data type "Parse" methods. 一种使用目标数据类型“解析”方法的方法。 But it may not be available for other data types. 但是它可能不适用于其他数据类型。 How can I construct object? 如何构造对象? Is there any method available in framework? 框架中是否有可用的方法?

The problem you're facing is that you are attempting to assign a value to a variable where the variable is an object data type. 您面临的问题是您试图将值分配给变量,而该变量是对象数据类型。

int i = 123; // This is fine as it assigns the primitive value 123 to the integer variable i.
string s = "123"; // This is also fine as you're assigning the string value "123" to the string variable s.
string t = s; // This is fine as long as variable s is a string datatype.

Your code is attempting to assign a value to an object datatype. 您的代码正在尝试为对象数据类型分配一个值。

testObj.PurchasedOn = "17-09-2016 18:50:00"; // This won't work as you cannot assign a string constant to a DateTime variable.

As you mention, you could use the Parse method if it is available. 如您所述,可以使用Parse方法(如果可用)。

If we look at the code you are expecting to produce it would most likely look like this: 如果我们查看您期望产生的代码,则很可能是这样的:

testObj.PurchasedOn = new DateTime(2016, 09, 17, 18, 50, 0);

As you can see, for the DateTime object constructor you would need to specify 6 parameters. 如您所见,对于DateTime对象构造函数,您需要指定6个参数。 This will obviously vary for every object type you wish to create. 对于您要创建的每种对象类型,这显然会有所不同。

The solution lies in the CodeObjectCreateExpression class which can be used in place of the CodePrimitiveExpression class. 解决方案位于CodeObjectCreateExpression类中,该类可以代替CodePrimitiveExpression类使用。

I would suggest changing your method to accept a CodeExpression rather than object propValue . 我建议将您的方法更改为接受CodeExpression而不是object propValue That way you can provide a primitive or an object instantiator. 这样,您可以提供原语或对象实例化器。

In this case you could pass to your method: 在这种情况下,您可以传递给您的方法:

new CodeObjectCreateExpression(typeof(DateTime), 2016, 09, 17, 18, 50, 0);

You can find more details on CodeObjectCreateExpression here . 您可以在此处找到有关CodeObjectCreateExpression更多详细信息。

What if you just get rid of your conditional and do: 如果您只是摆脱条件而去做,该怎么办:

private static CodeAssignStatement setProp(string propName, object propValue, Type propType, Type objType)
{
    CodeAssignStatement declareVariableName = null;
    declareVariableName = new CodeAssignStatement(
        new CodePropertyReferenceExpression(new CodeVariableReferenceExpression("testObj"), propName),
        new CodePrimitiveExpression(propValue)
    );

    return declareVariableName;
}

CodePrimitiveExpression appears to take in an object , which means you can assign pretty much anything to it. CodePrimitiveExpression似乎接受一个对象 ,这意味着您可以为其分配几乎任何东西。 As such, if you pass in a DateTime, it will be correctly stored as such. 这样,如果您传递DateTime,它将被正确存储。

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

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