Struct() public method

public Struct ( string name ) : TypeGen
name string
return TypeGen
        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 #2
0
        // 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");
				}
			}
		}
Example #3
0
		// 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 #4
0
        // 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 #5
0
        // 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);
				}
			}
		}