public static void ProcessConstruction(HLAobjectRoot obj, Type type) { object[] classAttributes = type.GetCustomAttributes(typeof(HLAObjectClassAttribute), false); if (classAttributes.Length == 0) { throw new Exception(type.FullName + " is class derived from HLAobjectRoot, but it hasn't the attribute HLAObjectClass"); } ConstructorInfo[] constructors = type.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); bool defaultConstructorExists = false; foreach (ConstructorInfo constrInfo in constructors) { if (constrInfo.IsPublic) { // TODO: según la guía de estilo debería lanzarse MyException en vez de Exception throw new Exception(type.FullName + " has the attribute HLAObjectClass but its constructor: " + constrInfo + " is public. Must change it to protected and call NewInstance(typeof(" + type.FullName + ")) instead"); } if (constrInfo.GetParameters().Length == 0) { defaultConstructorExists = true; } } // TODO: según la guía de estilo debería lanzarse MyException en vez de Exception if (!defaultConstructorExists) { throw new Exception(type.FullName + " must have a constructor by default without parameters"); } /* This code has moved to PreProcessConstruction in HLAobjectRoot * PropertyInfo[] infos = obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); * foreach (PropertyInfo propInfo in obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)) * { * object[] arrayOfCustomAttributes = propInfo.GetCustomAttributes(true); * foreach (Attribute attr in arrayOfCustomAttributes) * { * if (attr.GetType() == typeof(HLAAttributeAttribute)) * { * MethodInfo mi = propInfo.GetSetMethod(); * //Every HLAAttribute must be on a property declared as virtual and have a set method * if (mi != null && mi.IsVirtual) * { * Console.WriteLine(">>> Method info : " + mi + ", " + propInfo.DeclaringType.FullName); * ((HLAAttributeAttribute)attr).propInfo = propInfo; * obj.tableMethodInfo2Attr.Add(mi.Name, (HLAAttributeAttribute)attr); * obj.AttrTable.Add(((HLAAttributeAttribute)attr).Name, (HLAAttributeAttribute)attr); * ((HLAAttributeAttribute)attr).realobject = obj; * } * else * throw new Exception(propInfo.DeclaringType + "." + propInfo.Name + " property has the attribute HLAAttribute but it must be virtual and have a set method"); * } * } * } */ }
public static void ProcessConstruction(HLAobjectRoot obj, Type type) { object[] classAttributes = type.GetCustomAttributes(typeof(HLAObjectClassAttribute), false); if (classAttributes.Length == 0) { throw new Exception(type.FullName + " is class derived from HLAobjectRoot, but it hasn't the attribute HLAObjectClass"); } ConstructorInfo[] constructors = type.GetConstructors(BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance); bool defaultConstructorExists = false; foreach (ConstructorInfo constrInfo in constructors) { if (constrInfo.IsPublic) { // TODO: según la guía de estilo debería lanzarse MyException en vez de Exception throw new Exception(type.FullName + " has the attribute HLAObjectClass but its constructor: " + constrInfo + " is public. Must change it to protected and call NewInstance(typeof(" + type.FullName + ")) instead"); } if (constrInfo.GetParameters().Length == 0) defaultConstructorExists = true; } // TODO: según la guía de estilo debería lanzarse MyException en vez de Exception if (!defaultConstructorExists) throw new Exception(type.FullName + " must have a constructor by default without parameters"); /* This code has moved to PreProcessConstruction in HLAobjectRoot PropertyInfo[] infos = obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public); foreach (PropertyInfo propInfo in obj.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public)) { object[] arrayOfCustomAttributes = propInfo.GetCustomAttributes(true); foreach (Attribute attr in arrayOfCustomAttributes) { if (attr.GetType() == typeof(HLAAttributeAttribute)) { MethodInfo mi = propInfo.GetSetMethod(); //Every HLAAttribute must be on a property declared as virtual and have a set method if (mi != null && mi.IsVirtual) { Console.WriteLine(">>> Method info : " + mi + ", " + propInfo.DeclaringType.FullName); ((HLAAttributeAttribute)attr).propInfo = propInfo; obj.tableMethodInfo2Attr.Add(mi.Name, (HLAAttributeAttribute)attr); obj.AttrTable.Add(((HLAAttributeAttribute)attr).Name, (HLAAttributeAttribute)attr); ((HLAAttributeAttribute)attr).realobject = obj; } else throw new Exception(propInfo.DeclaringType + "." + propInfo.Name + " property has the attribute HLAAttribute but it must be virtual and have a set method"); } } } */ }
protected override void PostProceed(IInvocation invocation, ref object returnValue, params object[] arguments) { //Console.WriteLine("In PostProceed " + method); if (isSetMethodForHLAobjectRoot(invocation)) { HLAobjectRoot obj = invocation.InvocationTarget as HLAobjectRoot; HLAAttributeAttribute hlaAttr; System.Reflection.MethodInfo methodInfo = invocation.Method; if (obj.tableMethodInfo2Attr.TryGetValue(methodInfo.Name, out hlaAttr)) { hlaAttr.FlushAttributeValues(hlaAttr.AttributeInfo.Name, arguments); } } }
public override object Intercept(IInvocation invocation, params object[] args) { object retValue = null; if (isSetMethodForHLAobjectRoot(invocation) && !isCallbackInvocation()) { PreProceed(invocation, args); HLAobjectRoot obj = invocation.InvocationTarget as HLAobjectRoot; Sxtafederate federate = (Sxtafederate)obj.OwnFederate; if (federate == null || !(federate.HLAisJoined && federate.HLAtimeRegulating)) { retValue = invocation.Proceed(args); } PostProceed(invocation, ref retValue, args); } else { retValue = invocation.Proceed(args); } return(retValue); }
private static object NewInstance(Type type, HLAProxyAttribute proxyInfo, params object[] args) { // type == typeof(HLAobjectRoot) || if (type.IsSubclassOf(typeof(HLAobjectRoot))) { object obj; /* 1. Creates a new object */ if (args != null) { obj = _generator.CreateClassProxy(type, new HLAProxySink2(), args); } else { obj = _generator.CreateClassProxy(type, new HLAProxySink2()); } /* 2. Sets-up properties for the new object */ HLAobjectRoot objRoot = obj as HLAobjectRoot; //if (objRoot.OwnFederateAmbassador == null) // return obj; // It is a true proxy for a remote object if (proxyInfo != null) { objRoot.HLAprivilegeToDeleteObject_ = false; objRoot.instanceHandle = new XRTIObjectInstanceHandle(proxyInfo.ObjectInstanceHandle); objRoot.objectName = proxyInfo.ObjectName; objRoot.classHandle = new XRTIObjectClassHandle(proxyInfo.ObjectClassHandle); } // It is a local object else { objRoot.HLAprivilegeToDeleteObject_ = true; objRoot.OwnFederate = HLAobjectRoot.DefaultFederate; objRoot.OwnFederateAmbassador = HLAobjectRoot.DefaultFederateAmbassador; objRoot.OwnFederationExecutionHandle = HLAobjectRoot.DefaultFederationExecutionHandle; objRoot.instanceHandle = new XRTIObjectInstanceHandle(((BaseAmbassador)objRoot.OwnFederateAmbassador).HandleCounter); string className = obj.GetType().BaseType.Name; objRoot.objectName = className + objRoot.InstanceHandle.ToString(); } /* 3. Executes reflection process for aop engine */ HLAProxySink2.ProcessConstruction(objRoot, type); /* 4. Warns to the creation listeners */ foreach (IHLACreateObjectRootListener l in creationListeners) { l.OnReceiveCreatedNewObject(obj); } // It is a local object if (proxyInfo == null) { // COMMENT ANGEL: Permite enviar la modificación de propiedades que se produce en el constructor objRoot.FlushAttributeValues(null); objRoot.AutoFlushDisabled = false; } if (log.IsDebugEnabled) { log.Debug("Created an instance of " + obj.GetType().BaseType.Name + " [" + objRoot.InstanceHandle + "]"); } return(obj); } else { throw new Exception("Must be a subclass of HLAobjectRoot"); } }