public static void StructImpl(AssemblyGen ag, bool impl) { ITypeMapper m = ag.TypeMapper; TypeGen Test = ag.Struct("Test"); { CodeGen g = Test.Public.Static.Method(typeof(void), "Main"); { // test calling virtual member directly on a literal // g.Local(Operand.FromObject(3).Invoke("GetHashCode")); // test special case where the value type target doesn't implement the virtual function var value = g.Local(Test); g.InitObj(value); g.WriteLine("Hash code of {0} is {1}", value, value.Invoke("GetHashCode")); } if (impl) { g = Test.Public.Override.Method(typeof(int), "GetHashCode"); { g.Return(-1); } } } }
// example based on the MSDN Structs Sample (struct2.cs) public static void GenStruct2(AssemblyGen ag) { var st = ag.StaticFactory; var exp = ag.ExpressionFactory; TypeGen TheClass = ag.Class("TheClass"); { TheClass.Public.Field(typeof(int), "x"); } TypeGen TheStruct = ag.Struct("TheStruct"); { TheStruct.Public.Field(typeof(int), "x"); } TypeGen TestClass = ag.Class("TestClass"); { CodeGen g = TestClass.Public.Static.Method(typeof(void), "structtaker").Parameter(TheStruct, "s"); { ITypeMapper typeMapper = ag.TypeMapper; g.Assign(g.Arg("s").Field("x"), 5); } g = TestClass.Public.Static.Method(typeof(void), "classtaker").Parameter(TheClass, "c"); { ITypeMapper typeMapper = ag.TypeMapper; g.Assign(g.Arg("c").Field("x"), 5); } g = TestClass.Public.Static.Method(typeof(void), "Main"); { var a = g.Local(TheStruct); g.InitObj(a); ITypeMapper typeMapper4 = ag.TypeMapper; var b = g.Local(exp.New(TheClass)); ITypeMapper typeMapper = ag.TypeMapper; g.Assign(a.Field("x"), 1); ITypeMapper typeMapper2 = ag.TypeMapper; g.Assign(b.Field("x"), 1); g.Invoke(TestClass, "structtaker", a); g.Invoke(TestClass, "classtaker", b); ITypeMapper typeMapper3 = ag.TypeMapper; g.WriteLine("a.x = {0}", a.Field("x")); ITypeMapper typeMapper1 = ag.TypeMapper; g.WriteLine("b.x = {0}", b.Field("x")); } } }
// example based on the MSDN Structs Sample (struct1.cs) public static void GenStruct1(AssemblyGen ag) { var st = ag.StaticFactory; var exp = ag.ExpressionFactory; CodeGen g; TypeGen SimpleStruct = ag.Struct("SimpleStruct"); { FieldGen xval = SimpleStruct.Field(typeof(int), "xval"); PropertyGen X = SimpleStruct.Public.Property(typeof(int), "X"); { X.Getter().GetCode().Return(xval); g = X.Setter(); { g.If(g.PropertyValue() < 100); { g.Assign(xval, g.PropertyValue()); } g.End(); } } g = SimpleStruct.Public.Method(typeof(void), "DisplayX"); { g.WriteLine("The stored value is: {0}", xval); } } TypeGen TestClass = ag.Class("TestClass"); { g = TestClass.Public.Static.Method(typeof(void), "Main"); { var ss = g.Local(SimpleStruct); g.InitObj(ss); ITypeMapper typeMapper = ag.TypeMapper; g.Assign(ss.Property("X"), 5); g.Invoke(ss, "DisplayX"); } } }
public static void GenExtendedTest(AssemblyGen ag) { TypeGen Test = ag.Struct("Test"); { CodeGen g = Test.Static.Method(typeof(void), "Main"); { // test calling virtual member directly on a literal // g.Local(Operand.FromObject(3).Invoke("GetHashCode")); // test special case where the value type target doesn't implement the virtual function Operand value = g.Local(Test); g.InitObj(value); g.WriteLine("Hash code of {0} is {1}", value, value.Invoke("GetHashCode")); } g = Test.Public.Override.Method(typeof(int), "GetHashCode"); { g.Return(-1); } } }
// example based on the MSDN Structs Sample (struct2.cs) public static void GenStruct2(AssemblyGen ag) { TypeGen TheClass = ag.Class("TheClass"); { TheClass.Public.Field(typeof(int), "x"); } TypeGen TheStruct = ag.Struct("TheStruct"); { TheStruct.Public.Field(typeof(int), "x"); } TypeGen TestClass = ag.Class("TestClass"); { CodeGen g = TestClass.Public.Static.Method(typeof(void), "structtaker").Parameter(TheStruct, "s"); { g.Assign(g.Arg("s").Field("x"), 5); } g = TestClass.Public.Static.Method(typeof(void), "classtaker").Parameter(TheClass, "c"); { g.Assign(g.Arg("c").Field("x"), 5); } g = TestClass.Public.Static.Method(typeof(void), "Main"); { Operand a = g.Local(TheStruct); g.InitObj(a); Operand b = g.Local(Exp.New(TheClass)); g.Assign(a.Field("x"), 1); g.Assign(b.Field("x"), 1); g.Invoke(TestClass, "structtaker", a); g.Invoke(TestClass, "classtaker", b); g.WriteLine("a.x = {0}", a.Field("x")); g.WriteLine("b.x = {0}", b.Field("x")); } } }
// example based on the MSDN Structs Sample (struct2.cs) public static void GenStruct2(AssemblyGen ag) { TypeGen TheClass = ag.Class("TheClass"); { TheClass.Public.Field <int>("x"); } TypeGen TheStruct = ag.Struct("TheStruct"); { TheStruct.Public.Field <int>("x"); } TypeGen TestClass = ag.Class("TestClass"); { CodeGen g = TestClass.Public.Static.Void("structtaker").Parameter(TheStruct, "s"); { g.Assign(g.Arg("s").Field("x"), 5); } g = TestClass.Public.Static.Void("classtaker").Parameter(TheClass, "c"); { g.Assign(g.Arg("c").Field("x"), 5); } g = TestClass.Public.Static.Void("Main"); { Operand a = g.Local(TheStruct); g.InitObj(a); Operand b = g.Local(Exp.New(TheClass)); g.Assign(a.Field("x"), 1); g.Assign(b.Field("x"), 1); g.Invoke(TestClass, "structtaker", a); g.Invoke(TestClass, "classtaker", b); g.WriteLine("a.x = {0}", a.Field("x")); g.WriteLine("b.x = {0}", b.Field("x")); } } }
// example based on the MSDN Structs Sample (struct1.cs) public static void GenStruct1(AssemblyGen ag) { CodeGen g; TypeGen SimpleStruct = ag.Struct("SimpleStruct"); { FieldGen xval = SimpleStruct.Field(typeof(int), "xval"); PropertyGen X = SimpleStruct.Public.Property(typeof(int), "X"); { X.Getter().GetCode().Return(xval); g = X.Setter(); { g.If(g.PropertyValue() < 100); { g.Assign(xval, g.PropertyValue()); } g.End(); } } g = SimpleStruct.Public.Method(typeof(void), "DisplayX"); { g.WriteLine("The stored value is: {0}", xval); } } TypeGen TestClass = ag.Class("TestClass"); { g = TestClass.Public.Static.Method(typeof(void), "Main"); { Operand ss = g.Local(SimpleStruct); g.InitObj(ss); g.Assign(ss.Property("X"), 5); g.Invoke(ss, "DisplayX"); } } }
// example based on the MSDN Structs Sample (struct1.cs) public static void GenStruct1(AssemblyGen ag) { CodeGen g; TypeGen SimpleStruct = ag.Struct("SimpleStruct"); { FieldGen xval = SimpleStruct.Field <int>("xval"); PropertyGen X = SimpleStruct.Public.Property <int>("X"); { X.Getter().GetCode().Return(xval); g = X.Setter(); { g.If(g.PropertyValue() < 100); { g.Assign(xval, g.PropertyValue()); } g.End(); } } g = SimpleStruct.Public.Void("DisplayX"); { g.WriteLine("The stored value is: {0}", xval); } } TypeGen TestClass = ag.Class("TestClass"); { g = TestClass.Public.Static.Void("Main"); { Operand ss = g.Local(SimpleStruct); g.InitObj(ss); g.Assign(ss.Property("X"), 5); g.Invoke(ss, "DisplayX"); } } }
// example based on the MSDN User-Defined Conversions Sample (conversion.cs) public static void GenConversion(AssemblyGen ag) { var st = ag.StaticFactory; var exp = ag.ExpressionFactory; TypeGen RomanNumeral = ag.Struct("RomanNumeral"); { FieldGen value = RomanNumeral.Private.Field(typeof(int), "value"); CodeGen g = RomanNumeral.Public.Constructor().Parameter(typeof(int), "value"); { g.Assign(value, g.Arg("value")); } // Declare a conversion from an int to a RomanNumeral. Note the // the use of the operator keyword. This is a conversion // operator named RomanNumeral: g = RomanNumeral.Public.ImplicitConversionFrom(typeof(int)); { // Note that because RomanNumeral is declared as a struct, // calling new on the struct merely calls the constructor // rather than allocating an object on the heap: g.Return(exp.New(RomanNumeral, g.Arg("value"))); } // Declare an explicit conversion from a RomanNumeral to an int: g = RomanNumeral.Public.ExplicitConversionTo(typeof(int), "roman"); { ITypeMapper typeMapper = ag.TypeMapper; g.Return(g.Arg("roman").Field("value")); } // Declare an implicit conversion from a RomanNumeral to // a string: g = RomanNumeral.Public.ImplicitConversionTo(typeof(string)); { g.Return("Conversion not yet implemented"); } } TypeGen Test = ag.Class("Test"); { CodeGen g = Test.Public.Static.Method(typeof(void), "Main"); { var numeral = g.Local(RomanNumeral); g.Assign(numeral, 10); // Call the explicit conversion from numeral to int. Because it is // an explicit conversion, a cast must be used: g.WriteLine(numeral.Cast(typeof(int))); // Call the implicit conversion to string. Because there is no // cast, the implicit conversion to string is the only // conversion that is considered: g.WriteLine(numeral); // Call the explicit conversion from numeral to int and // then the explicit conversion from int to short: var s = g.Local(numeral.Cast(typeof(short))); g.WriteLine(s); } } }
// example based on the MSDN User-Defined Conversions Sample (structconversion.cs) public static void GenStructConversion(AssemblyGen ag) { var st = ag.StaticFactory; var exp = ag.ExpressionFactory; TypeGen BinaryNumeral = ag.Struct("BinaryNumeral"); { FieldGen value = BinaryNumeral.Private.Field(typeof(int), "value"); CodeGen g = BinaryNumeral.Public.Constructor().Parameter(typeof(int), "value"); { g.Assign(value, g.Arg("value")); } g = BinaryNumeral.Public.ImplicitConversionFrom(typeof(int)); { g.Return(exp.New(BinaryNumeral, g.Arg("value"))); } g = BinaryNumeral.Public.ImplicitConversionTo(typeof(string)); { g.Return("Conversion not yet implemented"); } g = BinaryNumeral.Public.ExplicitConversionTo(typeof(int), "binary"); { ITypeMapper typeMapper = ag.TypeMapper; g.Return(g.Arg("binary").Field("value")); } } TypeGen RomanNumeral = ag.Struct("RomanNumeral"); { FieldGen value = RomanNumeral.Private.Field(typeof(int), "value"); CodeGen g = RomanNumeral.Public.Constructor().Parameter(typeof(int), "value"); { g.Assign(value, g.Arg("value")); } g = RomanNumeral.Public.ImplicitConversionFrom(typeof(int)); { g.Return(exp.New(RomanNumeral, g.Arg("value"))); } g = RomanNumeral.Public.ImplicitConversionFrom(BinaryNumeral, "binary"); { g.Return(exp.New(RomanNumeral, g.Arg("binary").Cast(typeof(int)))); } g = RomanNumeral.Public.ExplicitConversionTo(typeof(int), "roman"); { ITypeMapper typeMapper = ag.TypeMapper; g.Return(g.Arg("roman").Field("value")); } g = RomanNumeral.Public.ImplicitConversionTo(typeof(string)); { g.Return("Conversion not yet implemented"); } } TypeGen Test = ag.Class("Test"); { CodeGen g = Test.Public.Static.Method(typeof(void), "Main"); { var roman = g.Local(RomanNumeral); g.Assign(roman, 10); var binary = g.Local(BinaryNumeral); // Perform a conversion from a RomanNumeral to a // BinaryNumeral: g.Assign(binary, roman.Cast(typeof(int)).Cast(BinaryNumeral)); // Performs a conversion from a BinaryNumeral to a RomanNumeral. // No cast is required: g.Assign(roman, binary); g.WriteLine(binary.Cast(typeof(int))); g.WriteLine(binary); } } }
// example based on the MSDN User-Defined Conversions Sample (structconversion.cs) public static void GenStructConversion(AssemblyGen ag) { TypeGen BinaryNumeral = ag.Struct("BinaryNumeral"); { FieldGen value = BinaryNumeral.Private.Field <int>("value"); CodeGen g = BinaryNumeral.Public.Constructor().Parameter <int>("value"); { g.Assign(value, g.Arg("value")); } g = BinaryNumeral.Public.ImplicitConversionFrom <int>(); { g.Return(Exp.New(BinaryNumeral, g.Arg("value"))); } g = BinaryNumeral.Public.ImplicitConversionTo <string>(); { g.Return("Conversion not yet implemented"); } g = BinaryNumeral.Public.ExplicitConversionTo <int>("binary"); { g.Return(g.Arg("binary").Field("value")); } } TypeGen RomanNumeral = ag.Struct("RomanNumeral"); { FieldGen value = RomanNumeral.Private.Field <int>("value"); CodeGen g = RomanNumeral.Public.Constructor().Parameter <int>("value"); { g.Assign(value, g.Arg("value")); } g = RomanNumeral.Public.ImplicitConversionFrom <int>(); { g.Return(Exp.New(RomanNumeral, g.Arg("value"))); } g = RomanNumeral.Public.ImplicitConversionFrom(BinaryNumeral, "binary"); { g.Return(Exp.New(RomanNumeral, g.Arg("binary").Cast <int>())); } g = RomanNumeral.Public.ExplicitConversionTo <int>("roman"); { g.Return(g.Arg("roman").Field("value")); } g = RomanNumeral.Public.ImplicitConversionTo <string>(); { g.Return("Conversion not yet implemented"); } } TypeGen Test = ag.Class("Test"); { CodeGen g = Test.Public.Static.Void("Main"); { Operand roman = g.Local(RomanNumeral); g.Assign(roman, 10); Operand binary = g.Local(BinaryNumeral); // Perform a conversion from a RomanNumeral to a // BinaryNumeral: g.Assign(binary, roman.Cast <int>().Cast(BinaryNumeral)); // Performs a conversion from a BinaryNumeral to a RomanNumeral. // No cast is required: g.Assign(roman, binary); g.WriteLine(binary.Cast <int>()); g.WriteLine(binary); } } }
// example based on the MSDN User-Defined Conversions Sample (conversion.cs) public static void GenConversion(AssemblyGen ag) { TypeGen RomanNumeral = ag.Struct("RomanNumeral"); { FieldGen value = RomanNumeral.Private.Field(typeof(int), "value"); CodeGen g = RomanNumeral.Public.Constructor().Parameter(typeof(int), "value"); { g.Assign(value, g.Arg("value")); } // Declare a conversion from an int to a RomanNumeral. Note the // the use of the operator keyword. This is a conversion // operator named RomanNumeral: g = RomanNumeral.Public.ImplicitConversionFrom(typeof(int)); { // Note that because RomanNumeral is declared as a struct, // calling new on the struct merely calls the constructor // rather than allocating an object on the heap: g.Return(Exp.New(RomanNumeral, g.Arg("value"))); } // Declare an explicit conversion from a RomanNumeral to an int: g = RomanNumeral.Public.ExplicitConversionTo(typeof(int), "roman"); { g.Return(g.Arg("roman").Field("value")); } // Declare an implicit conversion from a RomanNumeral to // a string: g = RomanNumeral.Public.ImplicitConversionTo(typeof(string)); { g.Return("Conversion not yet implemented"); } } TypeGen Test = ag.Class("Test"); { CodeGen g = Test.Public.Static.Method(typeof(void), "Main"); { Operand numeral = g.Local(RomanNumeral); g.Assign(numeral, 10); // Call the explicit conversion from numeral to int. Because it is // an explicit conversion, a cast must be used: g.WriteLine(numeral.Cast(typeof(int))); // Call the implicit conversion to string. Because there is no // cast, the implicit conversion to string is the only // conversion that is considered: g.WriteLine(numeral); // Call the explicit conversion from numeral to int and // then the explicit conversion from int to short: Operand s = g.Local(numeral.Cast(typeof(short))); g.WriteLine(s); } } }
// example based on the MSDN User-Defined Conversions Sample (structconversion.cs) public static void GenStructConversion(AssemblyGen ag) { TypeGen BinaryNumeral = ag.Struct("BinaryNumeral"); { FieldGen value = BinaryNumeral.Private.Field(typeof(int), "value"); CodeGen g = BinaryNumeral.Public.Constructor().Parameter(typeof(int), "value"); { g.Assign(value, g.Arg("value")); } g = BinaryNumeral.Public.ImplicitConversionFrom(typeof(int)); { g.Return(Exp.New(BinaryNumeral, g.Arg("value"))); } g = BinaryNumeral.Public.ImplicitConversionTo(typeof(string)); { g.Return("Conversion not yet implemented"); } g = BinaryNumeral.Public.ExplicitConversionTo(typeof(int), "binary"); { g.Return(g.Arg("binary").Field("value")); } } TypeGen RomanNumeral = ag.Struct("RomanNumeral"); { FieldGen value = RomanNumeral.Private.Field(typeof(int), "value"); CodeGen g = RomanNumeral.Public.Constructor().Parameter(typeof(int), "value"); { g.Assign(value, g.Arg("value")); } g = RomanNumeral.Public.ImplicitConversionFrom(typeof(int)); { g.Return(Exp.New(RomanNumeral, g.Arg("value"))); } g = RomanNumeral.Public.ImplicitConversionFrom(BinaryNumeral, "binary"); { g.Return(Exp.New(RomanNumeral, g.Arg("binary").Cast(typeof(int)))); } g = RomanNumeral.Public.ExplicitConversionTo(typeof(int), "roman"); { g.Return(g.Arg("roman").Field("value")); } g = RomanNumeral.Public.ImplicitConversionTo(typeof(string)); { g.Return("Conversion not yet implemented"); } } TypeGen Test = ag.Class("Test"); { CodeGen g = Test.Public.Static.Method(typeof(void), "Main"); { Operand roman = g.Local(RomanNumeral); g.Assign(roman, 10); Operand binary = g.Local(BinaryNumeral); // Perform a conversion from a RomanNumeral to a // BinaryNumeral: g.Assign(binary, roman.Cast(typeof(int)).Cast(BinaryNumeral)); // Performs a conversion from a BinaryNumeral to a RomanNumeral. // No cast is required: g.Assign(roman, binary); g.WriteLine(binary.Cast(typeof(int))); g.WriteLine(binary); } } }