Using CodeDOM in .NET to generate an assignment statement inside a property of a class
-
Could anyone please help me out in auto-generating a property in a class in a following format: public System.Int64 RollNumber { get { System.Int64 rollNo = (System.Int64 )this["@roll_number"]; return rollNo; } } I am able to generate the property with the return statement and the other stuff, but i am not able to Generate the assignment statement i,e : System.Int64 rollNo = (System.Int64 )this["@roll_number"]; Please Help. Thanks, Ramesh
-
Could anyone please help me out in auto-generating a property in a class in a following format: public System.Int64 RollNumber { get { System.Int64 rollNo = (System.Int64 )this["@roll_number"]; return rollNo; } } I am able to generate the property with the return statement and the other stuff, but i am not able to Generate the assignment statement i,e : System.Int64 rollNo = (System.Int64 )this["@roll_number"]; Please Help. Thanks, Ramesh
-
rameshbhojan wrote:
auto-generating
? Did you mean this?
public System.Int64 RollNumber
{
get
{
System.Int64 rollNo = (System.Int64 )this["@roll_number"];
return rollNo;
}
set
{
this["@roll_number"] = (proper_cast)value;
}
}Greetings - Gajatko
Hi Gajatko, All i wanna generate is : public System.Int64 RollNumber { get { System.Int64 rollNo = (System.Int64 )this["@roll_number"]; return rollNo; } } i don need the set statement.... anyways gimme the solution you have...... if i am able to generate the way you said then also it is fine!!!! Thanks, Ramesh
-
Could anyone please help me out in auto-generating a property in a class in a following format: public System.Int64 RollNumber { get { System.Int64 rollNo = (System.Int64 )this["@roll_number"]; return rollNo; } } I am able to generate the property with the return statement and the other stuff, but i am not able to Generate the assignment statement i,e : System.Int64 rollNo = (System.Int64 )this["@roll_number"]; Please Help. Thanks, Ramesh
The class your looking for is CodeVariableDeclarationStatement.
CodeMemberProperty prop = new CodeMemberProperty();
prop.Name = "RollNumber";
prop.HasGet = true;
prop.Attributes = MemberAttributes.Public;
prop.Type = new CodeTypeReference("System.Int64");prop.GetStatements.Add(new CodeVariableDeclarationStatement("System.Int64", "rollNo",
new CodeCastExpression("System.Int64",
new CodeIndexerExpression(
new CodeThisReferenceExpression(),
new CodePrimitiveExpression(
"@roll_number")))));
prop.GetStatements.Add(new CodeMethodReturnStatement(new CodeVariableReferenceExpression("rollNo")));But why bother with the variable when you could just return (System.Int64 )this["@roll_number"];