private static void RegisterMetas(Assembly a) { List<Type> baseTypes = new List<Type>(); Type[] types = a.GetTypes(); foreach (Type type in types) { if (type.IsSubclassOf(typeof(behaviac.Agent)) || Utils.IsStaticType(type)) { if (!IsRegisterd(type)) { Attribute[] attributes = (Attribute[])type.GetCustomAttributes(typeof(behaviac.TypeMetaInfoAttribute), false); if (attributes.Length > 0) { TypeInfo_t typeInfo = new TypeInfo_t(); typeInfo.type = type; ms_agentTypes.Add(typeInfo); //find all the base type and ancestors Type ti = type.BaseType; while (ti != null && Utils.IsAgentType(ti)) { baseTypes.Add(ti); ti = ti.BaseType; } if (Utils.IsStaticType(type)) { behaviac.TypeMetaInfoAttribute typeMetaInfo = attributes[0] as behaviac.TypeMetaInfoAttribute; Agent.RegisterStaticClass(type, typeMetaInfo.DisplayName, typeMetaInfo.Description); } } } } } foreach (Type type in baseTypes) { if (!IsRegisterd(type)) { TypeInfo_t ft = ms_agentTypes.Find(delegate(TypeInfo_t t) { return t.type == type; }); //not found if (ft == null) { TypeInfo_t typeInfo = new TypeInfo_t(); typeInfo.type = type; typeInfo.bIsInherited = true; ms_agentTypes.Add(typeInfo); } } } ms_agentTypes.Sort(delegate(TypeInfo_t x, TypeInfo_t y) { if (x.bIsInherited && !y.bIsInherited) { return -1; } else if (!x.bIsInherited && y.bIsInherited) { return 1; } if (x.type.IsSubclassOf(y.type)) { return 1; } else if (y.type.IsSubclassOf(x.type)) { return -1; } return x.type.FullName.CompareTo(y.type.FullName); }); foreach (TypeInfo_t typeInfo in ms_agentTypes) { Type type = typeInfo.type; Debug.Check(Utils.IsAgentType(type) || Utils.IsStaticType(type)); RegisterType(type, true); } }
private void RegisterMetas(Assembly a) { List<Type> baseTypes = new List<Type>(); Type[] types = a.GetTypes(); foreach (Type type in types) { //if (type.IsSubclassOf(typeof(behaviac.Agent)) || Utils.IsStaticType(type)) { if (!IsRegisterd(type)) { Attribute[] attributes = (Attribute[])type.GetCustomAttributes(typeof(behaviac.TypeMetaInfoAttribute), false); if (attributes.Length > 0) { TypeInfo_t typeInfo = new TypeInfo_t(); typeInfo.type = type; m_agentTypes.Add(typeInfo); CollectBaseTypes(baseTypes, type); if (Utils.IsStaticType(type)) { behaviac.TypeMetaInfoAttribute typeMetaInfo = attributes[0] as behaviac.TypeMetaInfoAttribute; Agent.RegisterStaticClass(type, typeMetaInfo.DisplayName, typeMetaInfo.Description); } } } } } foreach (Type type in baseTypes) { if (!IsRegisterd(type)) { TypeInfo_t typeInfo = new TypeInfo_t(); typeInfo.type = type; typeInfo.bIsInherited = true; m_agentTypes.Add(typeInfo); } } SortAgentTypes(m_agentTypes); foreach (TypeInfo_t typeInfo in m_agentTypes) { Type type = typeInfo.type; //Debug.Check(Utils.IsAgentType(type) || Utils.IsStaticType(type)); RegisterType(type, true); } //Agent Type's members might reference other agent types which are not decorated by TypeMetaInfoAttribute //and collected to m_agentTypes_Referenced baseTypes.Clear(); //collect all its ancestor agent types if not registered yet foreach (TypeInfo_t typeInfo in m_agentTypes_Referenced) { Type type = typeInfo.type; if (!IsRegisterd(type)) { CollectBaseTypes(baseTypes, type); } } foreach (Type type in baseTypes) { if (!IsRegisterd_Referenced(type) && !IsRegisterd(type)) { TypeInfo_t typeInfo = new TypeInfo_t(); typeInfo.type = type; typeInfo.bIsInherited = true; m_agentTypes_Referenced.Add(typeInfo); } } SortAgentTypes(m_agentTypes_Referenced); m_agentTypes.AddRange(m_agentTypes_Referenced); m_agentTypes_Referenced.Clear(); //resort after referenced agent types are added SortAgentTypes(m_agentTypes); RegisterReferedTypes(); }
private void RegisterType(Type type, bool bExpandMembers) { bool bReferendAgentType = false; Attribute[] attributes = (Attribute[])type.GetCustomAttributes(typeof(behaviac.TypeMetaInfoAttribute), false); if (!bExpandMembers && attributes.Length == 0) { //type is a referenced agent type which is not decorated by TypeMetaInfoAttribute if (Utils.IsAgentType(type) && !IsRegisterd_Referenced(type) && !IsRegisterd(type)) { TypeInfo_t typeInfo = new TypeInfo_t(); typeInfo.type = type; m_agentTypes_Referenced.Add(typeInfo); bReferendAgentType = true; } } //agent type must have behaviac.AgentTypeAttribute if (!bExpandMembers || attributes.Length > 0 || bReferendAgentType) { behaviac.TypeMetaInfoAttribute cda = attributes.Length > 0 ? (behaviac.TypeMetaInfoAttribute)attributes[0] : null; string typeFullName = type.FullName.Replace('+', '.'); Agent.CTagObjectDescriptor objectDesc = Agent.GetDescriptorByName(typeFullName); if (type.BaseType != null && Utils.IsAgentType(type.BaseType)) { string baseTypeFullName = type.BaseType.FullName.Replace('+', '.'); Agent.CTagObjectDescriptor baseObjectDesc = Agent.GetDescriptorByName(baseTypeFullName); objectDesc.m_parent = baseObjectDesc; } objectDesc.type = type; objectDesc.displayName = ((cda == null || string.IsNullOrEmpty(cda.DisplayName)) ? typeFullName : cda.DisplayName); objectDesc.desc = ((cda == null || string.IsNullOrEmpty(cda.Description)) ? objectDesc.displayName : cda.Description); if (Utils.IsEnumType(type)) { return; } BindingFlags bindingFlags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance | BindingFlags.Static | BindingFlags.DeclaredOnly; FieldInfo[] fields = type.GetFields(bindingFlags); foreach (FieldInfo f in fields) { //for agent type, only register members with MemberDescAttribute behaviac.MemberMetaInfoAttribute memberDesc; bool bToRegister = IfToRegister(bExpandMembers, f, out memberDesc); if (bToRegister) { CMemberBase m = new CFieldInfo(f, memberDesc); RegisterMember(type, objectDesc, f.FieldType, m); } } PropertyInfo[] properties = type.GetProperties(bindingFlags); foreach (PropertyInfo p in properties) { //for agent type, only register members with MemberDescAttribute behaviac.MemberMetaInfoAttribute memberDesc; bool bToRegister = IfToRegister(bExpandMembers, p, out memberDesc); if (bToRegister) { CMemberBase m = new CProperyInfo(p, memberDesc); RegisterMember(type, objectDesc, p.PropertyType, m); } } if (bExpandMembers) { MethodInfo[] methods = type.GetMethods(bindingFlags); foreach (MethodInfo m in methods) { Attribute[] attributes2 = (Attribute[])m.GetCustomAttributes(typeof(behaviac.MethodMetaInfoAttribute), false); if (attributes2.Length > 0) { behaviac.MethodMetaInfoAttribute methodDesc = (behaviac.MethodMetaInfoAttribute)attributes2[0]; CMethodBase method = new CMethodBase(m, methodDesc, null); objectDesc.ms_methods.Add(method); ParameterInfo[] parameters = m.GetParameters(); foreach (ParameterInfo para in parameters) { if ((Utils.IsCustomClassType(para.ParameterType) || Utils.IsEnumType(para.ParameterType)) && !Agent.IsTypeRegisterd(para.ParameterType.FullName)) { RegisterType(para.ParameterType, false); } } if ((Utils.IsCustomClassType(m.ReturnType) || Utils.IsEnumType(m.ReturnType)) && !Agent.IsTypeRegisterd(m.ReturnType.FullName)) { RegisterType(m.ReturnType, false); } } }//end of foreach //Type[] delegates = type.GetNestedTypes(bindingFlags); //foreach (Type d in delegates) //{ // Attribute[] attributes0 = (Attribute[])d.GetCustomAttributes(typeof(behaviac.EventMetaInfoAttribute), false); // if (attributes0.Length > 0) // { // behaviac.EventMetaInfoAttribute eventDesc = (behaviac.EventMetaInfoAttribute)attributes0[0]; // MethodInfo m = d.GetMethod("Invoke"); // CNamedEvent method = new CNamedEvent(m, eventDesc, d.Name); // objectDesc.m_methods.Add(method); // } //} }//if (bIsAgentType) } }
public void AddAgentType(Type agentType, bool isInherited) { if (!IsRegisterd(agentType)) { TypeInfo_t typeInfo = new TypeInfo_t(); typeInfo.type = agentType; typeInfo.bIsInherited = isInherited; m_agentTypes.Add(typeInfo); } }