private static void TestMyStruct() { Console.WriteLine(MethodBase.GetCurrentMethod().Name); MyStruct foo = new MyStruct(); var resA1 = foo.GetAnnotation <MyStruct>(); var resA2 = foo.GetAnnotation <MyStruct>(nameof(MyStruct.MyString)); var resA3 = foo.GetAnnotation <MyStruct>("MyField"); Console.WriteLine($"{nameof(resA1)}: {resA1}"); Console.WriteLine($"{nameof(resA2)}: {resA2}"); Console.WriteLine($"{nameof(resA3)}: {resA3}"); foo = new MyStruct(); var resB1 = foo.GetAnnotation <MyStruct>(); var resB2 = foo.GetAnnotation <MyStruct>(nameof(MyStruct.MyString)); var resB3 = foo.GetAnnotation <MyStruct>("MyField"); Console.WriteLine($"{nameof(resB1)}: {resB1}"); Console.WriteLine($"{nameof(resB2)}: {resB2}"); Console.WriteLine($"{nameof(resB3)}: {resB3}"); var resC1 = typeof(MyStruct).GetAnnotation(); var resC2 = foo.GetType().GetAnnotation(); var resC3 = typeof(MyStruct).GetAnnotation("MyString"); var resC4 = foo.GetType().GetAnnotation("MyString"); Console.WriteLine($"{nameof(resC1)}: {resC1}"); Console.WriteLine($"{nameof(resC2)}: {resC2}"); Console.WriteLine($"{nameof(resC3)}: {resC3}"); Console.WriteLine($"{nameof(resC4)}: {resC4}"); }
static void Main() { var s = new MyStruct(); s.GetType().GetField("Field").SetValueDirect(__makeref(s), 5); System.Console.WriteLine(s.Field); //Prints 5 }
public static void PrintStructProperties(MyStruct s) { // NOTE: This code will not handle indexer properties... var publicProperties = s.GetType().GetProperties(); foreach (var prop in publicProperties) { Console.WriteLine("{0} = {1}", prop.Name, prop.GetValue(s, null)); } }
public void Run() { MyStruct s = new MyStruct(); TestApi.WriteLine(s.GetType().ToString()); TestApi.WriteLine(s.ToString()); MyEnum e = MyEnum.Default; //TestApi.WriteLine(e.CompareTo(e).ToString()); TestApi.WriteLine(e.GetTypeCode().ToString()); TestApi.WriteLine(e.ToString()); }
private static MyStruct FromArray(int[] arr) { IntPtr ptr = IntPtr.Zero; try { MyStruct str = new MyStruct(); int size = GetInt32ArraySize(str); ptr = Marshal.AllocHGlobal(size); Marshal.Copy(arr, 0, ptr, size); str = (MyStruct)Marshal.PtrToStructure(ptr, str.GetType()); return(str); } finally { Marshal.FreeHGlobal(ptr); } }