System.CodeDom.Tests.CSharpCodeGenerationTests.CastingOperations C# (CSharp) Method

CastingOperations() private method

private CastingOperations ( ) : void
return void
        public void CastingOperations()
        {
            var cd = new CodeTypeDeclaration();
            cd.Name = "Test";
            cd.IsClass = true;

            // create method to test casting float to int
            CodeMemberMethod castReturnValue = new CodeMemberMethod();
            castReturnValue.Name = "CastReturnValue";
            castReturnValue.ReturnType = new CodeTypeReference(typeof(int));
            castReturnValue.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            CodeParameterDeclarationExpression strParam = new CodeParameterDeclarationExpression(typeof(string), "value");
            castReturnValue.Parameters.Add(strParam);
            castReturnValue.Statements.Add(new CodeMethodReturnStatement(new CodeCastExpression(typeof(int), new CodeMethodInvokeExpression(new CodeTypeReferenceExpression("System.Single"), "Parse", new CodeExpression[] { new CodeVariableReferenceExpression("value"), new CodePropertyReferenceExpression(new CodeTypeReferenceExpression("System.Globalization.CultureInfo"), "InvariantCulture") }))));
            cd.Members.Add(castReturnValue);

            // create method to test casting interface -> class
            CodeMemberMethod castInterface = new CodeMemberMethod();
            castInterface.Name = "CastInterface";
            castInterface.ReturnType = new CodeTypeReference(typeof(string));
            castInterface.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            CodeParameterDeclarationExpression interfaceParam = new CodeParameterDeclarationExpression(typeof(System.ICloneable), "value");
            castInterface.Parameters.Add(interfaceParam);
            castInterface.Statements.Add(new CodeMethodReturnStatement(new CodeCastExpression(typeof(string), new CodeVariableReferenceExpression("value"))));
            cd.Members.Add(castInterface);

            // create method to test casting value type -> reference type
            CodeMemberMethod valueToReference = new CodeMemberMethod();
            valueToReference.Name = "ValueToReference";
            valueToReference.ReturnType = new CodeTypeReference(typeof(System.Object));
            valueToReference.Attributes = MemberAttributes.Public | MemberAttributes.Static;
            CodeParameterDeclarationExpression valueParam = new CodeParameterDeclarationExpression(typeof(int), "value");
            valueToReference.Parameters.Add(valueParam);
            valueToReference.Statements.Add(new CodeMethodReturnStatement(new CodeCastExpression(typeof(System.Object), new CodeVariableReferenceExpression("value"))));
            cd.Members.Add(valueToReference);

            AssertEqual(cd,
                @"public class Test {
                     public static int CastReturnValue(string value) {
                         return ((int)(float.Parse(value, System.Globalization.CultureInfo.InvariantCulture)));
                     }
                     public static string CastInterface(System.ICloneable value) {
                         return ((string)(value));
                     }
                     public static object ValueToReference(int value) {
                         return ((object)(value));
                     }
                 }");
        }