private void RegisterType(CKlaxScriptTypeInfo typeInfo) { Type type = typeInfo.Type; Types.Add(typeInfo); m_klaxTypeMap.Add(typeInfo.Type, typeInfo); foreach (var method in type.GetMethods()) { KlaxFunctionAttribute klaxFunction = method.GetCustomAttribute <KlaxFunctionAttribute>(); if (klaxFunction != null) { MethodInfo baseMethod = method.GetBaseDefinition(); if (baseMethod.DeclaringType == type) { typeInfo.Functions.Add(CreateFunction(klaxFunction, method)); } } } foreach (var property in type.GetProperties()) { KlaxPropertyAttribute klaxProperty = property.GetCustomAttribute <KlaxPropertyAttribute>(); if (klaxProperty != null) { if (property.DeclaringType == type) { typeInfo.Properties.Add(CreateProperty(klaxProperty, property)); } } } foreach (var field in type.GetFields()) { KlaxPropertyAttribute klaxProperty = field.GetCustomAttribute <KlaxPropertyAttribute>(); if (klaxProperty != null) { if (field.DeclaringType == type) { string name = klaxProperty.DisplayName ?? field.Name; typeInfo.Properties.Add(CreateProperty(field, name, klaxProperty.Category, klaxProperty.IsReadOnly)); } } else { KlaxEventAttribute klaxEvent = field.GetCustomAttribute <KlaxEventAttribute>(); if (klaxEvent != null && field.DeclaringType == type) { typeInfo.Events.Add(CreateEvent(field, klaxEvent)); } } } }
private CKlaxScriptPropertyInfo CreateProperty(KlaxPropertyAttribute attribute, PropertyInfo property) { CKlaxScriptPropertyInfo info = new CKlaxScriptPropertyInfo { propertyInfo = property, displayName = attribute.DisplayName ?? property.Name, category = attribute.Category, tooltip = attribute.Tooltip, declaringType = property.DeclaringType, propertyType = property.PropertyType, bIsReadOnly = attribute.IsReadOnly || !property.CanWrite, }; return(info); }
public static CObjectProperties GetObjectProperties(object instance) { if (instance == null) { return(null); } CObjectProperties properties = new CObjectProperties { target = instance }; Type type = instance.GetType(); foreach (var field in type.GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)) { KlaxPropertyAttribute attribute = field.GetCustomAttribute <KlaxPropertyAttribute>(); if (attribute != null && !attribute.IsReadOnly) { string name = attribute.DisplayName ?? field.Name; string categoryName = attribute.Category ?? "Default"; CCategoryInfo category = new CCategoryInfo() { Name = categoryName, Priority = attribute.CategoryPriority }; CObjectProperty property = new CObjectProperty(name, category, instance, field.GetValue(instance), field.FieldType, null, field); properties.properties.Add(property); } } foreach (var property in type.GetProperties(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)) { KlaxPropertyAttribute attribute = property.GetCustomAttribute <KlaxPropertyAttribute>(); if (attribute != null && !attribute.IsReadOnly) { string name = attribute.DisplayName ?? property.Name; string categoryName = attribute.Category ?? "Default"; CCategoryInfo category = new CCategoryInfo() { Name = categoryName, Priority = attribute.CategoryPriority }; CObjectProperty objectProperty = new CObjectProperty(name, category, instance, property.GetValue(instance), property.PropertyType, property, null); properties.properties.Add(objectProperty); } } foreach (var KlaxProperty in type.GetMethods(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)) { EditorFunctionAttribute attribute = KlaxProperty.GetCustomAttribute <EditorFunctionAttribute>(); if (attribute != null) { if (KlaxProperty.ReturnType != typeof(void) || KlaxProperty.GetParameters().Length > 0) { LogUtility.Log("The function {0} of Type {1} cannot be converted into a parameterless, void returning function. This is necessary for an editor function.", KlaxProperty.Name, type.Name); continue; } string name = attribute.DisplayName ?? KlaxProperty.Name; string categoryName = attribute.Category ?? "Default"; CCategoryInfo category = new CCategoryInfo() { Name = categoryName, Priority = attribute.CategoryPriority }; CObjectFunction objectFunction = new CObjectFunction(name, category, instance, KlaxProperty); properties.properties.Add(objectFunction); } } return(properties); }