public void TypeOf()
{
var nspace = new CodeNamespace("System.Something");
var class1 = new CodeTypeDeclaration("ClassToTest") { IsClass = true };
nspace.Types.Add(class1);
var cmm = new CodeMemberMethod();
cmm.Name = "Primitives";
cmm.ReturnType = new CodeTypeReference(typeof(string));
cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
cmm.Statements.Add(new CodeMethodReturnStatement(new CodeMethodInvokeExpression(new CodeTypeOfExpression(typeof(int)), "ToString")));
class1.Members.Add(cmm);
cmm = new CodeMemberMethod();
cmm.Name = "ArraysOfPrimitives";
cmm.ReturnType = new CodeTypeReference(typeof(string));
cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
cmm.Statements.Add(new CodeMethodReturnStatement(new CodeMethodInvokeExpression(new CodeTypeOfExpression(typeof(int[])), "ToString")));
class1.Members.Add(cmm);
cmm = new CodeMemberMethod();
cmm.Name = "NonPrimitives";
cmm.ReturnType = new CodeTypeReference(typeof(string));
cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
cmm.Statements.Add(new CodeMethodReturnStatement(new CodeMethodInvokeExpression(new CodeTypeOfExpression(typeof(System.ICloneable)), "ToString")));
class1.Members.Add(cmm);
cmm = new CodeMemberMethod();
cmm.Name = "ArraysOfNonPrimitives";
cmm.ReturnType = new CodeTypeReference(typeof(string));
cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
cmm.Statements.Add(new CodeMethodReturnStatement(new CodeMethodInvokeExpression(new CodeTypeOfExpression(typeof(System.ICloneable[])), "ToString")));
class1.Members.Add(cmm);
cmm = new CodeMemberMethod();
cmm.Name = "Enumerations";
cmm.ReturnType = new CodeTypeReference(typeof(string));
cmm.Attributes = MemberAttributes.Public | MemberAttributes.Static;
cmm.Statements.Add(new CodeMethodReturnStatement(new CodeMethodInvokeExpression(new CodeTypeOfExpression("DecimalEnum"), "ToString")));
class1.Members.Add(cmm);
var ce = new CodeTypeDeclaration("DecimalEnum") { IsEnum = true };
ce.IsEnum = true;
nspace.Types.Add(ce);
for (int k = 0; k < 5; k++)
{
CodeMemberField Field = new CodeMemberField("System.Int32", "Num" + (k).ToString());
Field.InitExpression = new CodePrimitiveExpression(k);
ce.Members.Add(Field);
}
AssertEqual(nspace,
@"namespace System.Something {
public class ClassToTest {
public static string Primitives() {
return typeof(int).ToString();
}
public static string ArraysOfPrimitives() {
return typeof(int[]).ToString();
}
public static string NonPrimitives() {
return typeof(System.ICloneable).ToString();
}
public static string ArraysOfNonPrimitives() {
return typeof(System.ICloneable[]).ToString();
}
public static string Enumerations() {
return typeof(DecimalEnum).ToString();
}
}
public enum DecimalEnum {
Num0 = 0,
Num1 = 1,
Num2 = 2,
Num3 = 3,
Num4 = 4,
}
}");
}