Here i the method 'BOOL()' returns a Boolean value. Now i need to do similar thing of returning a DateTime value. When i used,
new CodePrimitiveExpression(DATETIME());
where 'DATETIME()' returns a DateTime value. But i got the below exception when i tried CodePrimitiveExpression,
Invalid Primitive Type: System.DateTime. Consider using CodeObjectCreateExpression.
So how can i use CodeObjectExpression to get a DateTime value.?
Here is the code snippet attached.
if (p.ParameterType == typeof(System.Boolean))
return new CodeVariableDeclarationStatement(p.ParameterType, options.sVariableNamePrix + p.Name,
new CodePrimitiveExpression(BOOL()));
else if (p.ParameterType == typeof(System.DateTime))
return new CodeVariableDeclarationStatement(p.ParameterType, options.sVariableNamePrix + p.Name,
// new CodeObjectCreateExpression(DATETIME()));
-
You'll need to specify the type of the object (
DateTimein this case) and the parameter(s) to pass to its constructor.If you want the generated declaration to use the value returned from your
DATETIME()method then it's probably easiest to use theDateTime(long ticks, DateTimeKind kind)constructor.// ... else if (p.ParameterType == typeof(System.DateTime)) { DateTime dt = DATETIME(); var ticks = new CodePrimitiveExpression(dt.Ticks); var kind = new CodeFieldReferenceExpression ( new CodeTypeReferenceExpression("System.DateTimeKind"), dt.Kind.ToString() ); return new CodeVariableDeclarationStatement ( p.ParameterType, options.sVariableNamePrix + p.Name, new CodeObjectCreateExpression("System.DateTime", ticks, kind) ); }EDIT in response to comment...
If you want the generated declaration to use the value that was returned from your
DATETIME()method then that value needs to be passed into theDateTimeobject's constructor somehow.I chose the
DateTime(long ticks, DateTimeKind kind)constructor because it's the simplest constructor that allows you to accurately represent that value. (Using any of the other constructors would have made the example either more complicated or less accurate.)Using
TicksandDateTimeKindis just a different way to represent the sameDateTime:DateTime dt1 = new DateTime(630822816000000000, System.DateTimeKind.Unspecified); DateTime dt2 = DateTime.Parse("1/1/2000 12:00:00 AM"); Console.WriteLine(dt1); // displays "01/01/2000 00:00:00" Console.WriteLine(dt2); // displays "01/01/2000 00:00:00" Console.WriteLine(dt1 == dt2); // displays "True"pragadheesh : my DATETIME method returns something like this '1/1/2000 12:00:00 AM' which i need to use. Also can u explain what is 'ticks' and 'kind'. When i tried your code i'm getting something like this, '630822816000000000, System.DateTimeKind.Unspecified'
0 comments:
Post a Comment