C # script practice (4): Reflection and serialization

Source: Internet
Author: User
  • Serialization
    • This is relatively simple. Net supports it directly. XML serialization is used as an example:
      public static string SerializeToString (object obj){XmlSerializer serializer = new XmlSerializer (obj.GetType ()); using (StringWriter writer = new StringWriter()) {serializer.Serialize (writer, obj); return writer.ToString ();}}public static T SerializeFromString<T> (string xml){XmlSerializer serializer = new XmlSerializer (typeof(T));  using (StringReader reader = new StringReader(xml)) {return (T)serializer.Deserialize (reader);}}
      public struct Vector3{public float x;public float y;public float z;}public class TestClass{public int testInt = 1234;public float testFloat = 0.2341f;public bool[] boolArray = {true, false, false, true};public Vector3 position;public TestClass (){this.position.x = 100;this.position.y = 100;this.position.z = 0;}}

      The result is as follows:

      <?xml version="1.0" encoding="utf-8"?><TestClass xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">  <testInt>1234</testInt>  <testFloat>0.2341</testFloat>  <boolArray>    <boolean>true</boolean>    <boolean>false</boolean>    <boolean>false</boolean>    <boolean>true</boolean>  </boolArray>  <position>    <x>100</x>    <y>100</y>    <z>0</z>  </position></TestClass>

      At that time, we will use the mono API to upload the string to C ++. How can we store it? The binary format is the same.

  • Reflection
    • . Net also supports reflection. The question is how to reflect to C ++.
    • Use the mono API to obtain the meta information of the class and then respond to the editor to edit it.
    • If you want to be lazy, you can use mono to create a window, put a propertygrid control, and then embed the window into the main Editor interface.
      int depth = 0;void ListFields(MonoObject* object){assert(NULL != object);++depth;MonoClass* objectClass = mono_object_get_class(object);const char* className = mono_class_get_name(objectClass);printf("[%s]\n", className);void* iter = NULL;MonoClassField* field = NULL;while (field = mono_class_get_fields(objectClass, &iter)){for (int i = 0; i < depth; ++i){printf("\t");}const char* fieldName = mono_field_get_name(field);printf("%s : ", fieldName);MonoType* fieldType = mono_field_get_type(field);int type = mono_type_get_type(fieldType);switch (type){case MONO_TYPE_BOOLEAN:{bool boolValue = false;mono_field_get_value(object, field, &boolValue);printf("%s\n", boolValue ? "true" : "false");}break;case MONO_TYPE_I4:{int intValue = 0;mono_field_get_value(object, field, &intValue);printf("%d\n", intValue);}break;case MONO_TYPE_R4:{float floatValue = 0;mono_field_get_value(object, field, &floatValue);printf("%f\n", floatValue);}break;case MONO_TYPE_CHAR:case MONO_TYPE_I1:case MONO_TYPE_U1:case MONO_TYPE_I2:case MONO_TYPE_U2:case MONO_TYPE_U4:case MONO_TYPE_I:case MONO_TYPE_U:case MONO_TYPE_I8:case MONO_TYPE_U8:case MONO_TYPE_R8:{//@todo}break;case MONO_TYPE_SZARRAY:{MonoObject* value = mono_field_get_value_object(mono_domain_get(), field, object);MonoArray* array = (MonoArray*)value;uintptr_t size = mono_array_length(array);MonoClass* elementClass = mono_class_get_element_class(mono_object_get_class(value));MonoType* elementType = mono_class_get_type(elementClass);switch (mono_type_get_type(elementType)){case MONO_TYPE_BOOLEAN:{bool* data = mono_array_addr(array, bool, 0);for (int i = 0; i < size; ++i){printf("%s ", data[i] ? "true" : "false");}printf("\n");}break;default: //@todobreak;}className = mono_class_get_name(elementClass);}break;case MONO_TYPE_STRING:{MonoObject* value = mono_field_get_value_object(mono_domain_get(), field, object);MonoString* valueString = (MonoString*)value;const char* valueCStr = mono_string_to_utf8(valueString);printf("%s\n", valueCStr);}break;default:{MonoObject* value = mono_field_get_value_object(mono_domain_get(), field, object);ListFields(value);}break;}}--depth;}

      Output:

      [TestClass]        testBool : false        testInt : 1234        testFloat : 0.234100        testString : hello        boolArray : true false false true        position : [Vector3]                x : 100.000000                y : 100.000000                z : 0.000000

C # script practice (III): integrated into the game

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.