Example #1
0
        /// <summary>
        /// Creates a delegate for the specified method and target.
        /// </summary>
        /// <param name="delegateType">Type of the delegate.</param>
        /// <param name="targetType">Type of the target.</param>
        /// <param name="methodName">Name of the method.</param>
        /// <returns>The delegate.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="delegateType"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="targetType"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">The <paramref name="methodName"/> is <c>null</c> or whitespace.</exception>
        /// <remarks></remarks>
        public static Delegate CreateDelegate(Type delegateType, Type targetType, string methodName)
        {
            Argument.IsNotNull("delegateType", delegateType);
            Argument.IsNotNull("targetType", targetType);
            Argument.IsNotNullOrWhitespace("methodName", methodName);

            var methodInfo = targetType.GetMethodEx(methodName, BindingFlagsHelper.GetFinalBindingFlags(true, true));
            return CreateDelegate(delegateType, null, methodInfo);
        }
        private static Func<IEnumerable<Type>> FindAccessor(string resourceName, Type resourceType)
        {
            var methodInfo = resourceType.GetMethodEx(resourceName,
                MemberFlags.Static | MemberFlags.Public | MemberFlags.NonPublic);
            if (methodInfo == null)
                throw ExceptionManager.ResourceNotFound(resourceName, resourceType);

            if (!typeof(IEnumerable<Type>).IsAssignableFrom(methodInfo.ReturnType))
                throw ExceptionManager.ResourceHasNotGetter(resourceName, resourceType);
            return (Func<IEnumerable<Type>>)ServiceProvider.ReflectionManager.GetMethodDelegate(typeof(Func<IEnumerable<Type>>), methodInfo);
        }
        /// <summary>
        /// Gets the known types via attributes.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <returns>The list of known types via the <see cref="KnownTypeAttribute"/>.</returns>
        /// <exception cref="ArgumentNullException">The <paramref name="type"/> is <c>null</c>.</exception>
        protected virtual Type[] GetKnownTypesViaAttributes(Type type)
        {
            Argument.IsNotNull("type", type);

            string typeName = type.AssemblyQualifiedName;
            if (string.IsNullOrWhiteSpace(typeName))
            {
                return new Type[] { };
            }

            return _knownTypesByAttributesCache.GetFromCacheOrFetch(typeName, () =>
            {
                var additionalTypes = new List<Type>();
                var knownTypeAttributes = type.GetCustomAttributesEx(typeof(KnownTypeAttribute), true);
                foreach (var attr in knownTypeAttributes)
                {
                    var ktattr = attr as KnownTypeAttribute;
                    if (ktattr != null)
                    {
                        if (ktattr.MethodName != null)
                        {
                            var mi = type.GetMethodEx(ktattr.MethodName, BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);

                            // this can be null because we are also getting here through the recursive behaviour
                            // of GetCustomAttributesEx. We are getting at this point once per class derived from a
                            // base class having a KnownType() with a method. This can be ignored
                            if (mi != null)
                            {
                                var types = mi.Invoke(null, null) as IEnumerable<Type>;
                                if (types != null)
                                {
                                    additionalTypes.AddRange(types);
                                }
                            }
                        }
                        else
                        {
                            additionalTypes.Add(ktattr.Type);
                        }
                    }
                }

                return additionalTypes.ToArray();
            });
        }
        private void initFARModel()
        {
            Debug.Log("Trajectories: Initializing aerodynamic model...");

            bool farInstalled = false;

            foreach (var loadedAssembly in AssemblyLoader.loadedAssemblies)
            {
                try
                {
                    switch (loadedAssembly.name)
                    {
                        case "NEAR":
                            useNEAR = true;
                            goto case "FerramAerospaceResearch";
                        case "FerramAerospaceResearch":
                            string namespaceName = useNEAR ? "NEAR" : "FerramAerospaceResearch";
                            FARAPIType = loadedAssembly.assembly.GetType(namespaceName + ".FARAPI");
                            if (useNEAR)
                            {
                                throw new Exception("NEAR support not implemented yet");
                            }
                            else
                            {
                                // public static void FerramAerospaceResearch.FARAPI.CalculateVesselAeroForces(Vessel vessel, out Vector3 aeroForce, out Vector3 aeroTorque, Vector3 velocityWorldVector, double altitude)
                                FARAPI_CalculateVesselAeroForces = FARAPIType.GetMethodEx("CalculateVesselAeroForces", BindingFlags.Public | BindingFlags.Static, new Type[] { typeof(Vessel), typeof(Vector3).MakeByRefType(), typeof(Vector3).MakeByRefType(), typeof(Vector3), typeof(double) });

                                // public static double FerramAerospaceResearch.FARAeroUtil.GetCurrentDensity(CelestialBody body, double altitude, bool densitySmoothingAtOcean = true)
								FARAeroUtil_GetCurrentDensity = loadedAssembly.assembly.GetType(namespaceName + ".FARAeroUtil").GetMethodEx("GetCurrentDensity", new Type[] { typeof(CelestialBody), typeof(double), typeof(bool) });
                            }
                            farInstalled = true;
                            break;
                    }
                }
                catch(Exception e)
                {
                    Debug.Log("Trajectories: failed to interface with assembly " + loadedAssembly.name);
                    Debug.Log("Using stock model instead");
                    Debug.Log(e.ToString());
                }
            }

            if (!farInstalled)
            {
                //ScreenMessages.PostScreenMessage("Ferram Aerospace Research (FAR or NEAR) not installed, or incompatible version, using stock aerodynamics");
                //ScreenMessages.PostScreenMessage("WARNING: stock aerodynamic model does not predict lift, spacecrafts with wings will have inaccurate predictions");
                aerodynamicModelName_ = "stock";
                useStockModel = true;
                isValid = true;
            }
            else
            {
                if(useNEAR)
                    aerodynamicModelName_ = "NEAR";
                else
                    aerodynamicModelName_ = "FAR";
            }

            maxFARVelocity = 10000.0;
            maxFARAngleOfAttack = 180.0 / 180.0 * Math.PI;

            int velocityResolution = 32;
            int angleOfAttackResolution = 33; // even number to include exactly 0°
            int altitudeResolution = 32;

            cachedFARForces = new Vector2[velocityResolution, angleOfAttackResolution, altitudeResolution];

            for (int v = 0; v < velocityResolution; ++v)
            {
                for (int a = 0; a < angleOfAttackResolution; ++a)
                {
                    for (int m = 0; m < altitudeResolution; ++m)
                    {
                        cachedFARForces[v, a, m] = new Vector2(float.NaN, float.NaN);
                    }
                }
            }

            isValid = true;

            precomputeCache();

            return;
        }
Example #5
0
        /// <summary>
        /// Adds the type to schema set by reading the <see cref="XmlSchemaProviderAttribute" /> or by
        /// using the known schema sets information.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="schemaSet">The schema set.</param>
        /// <param name="serializationManager">The serialization manager.</param>
        /// <returns>The xml qualified name.</returns>
        private static XmlQualifiedName AddTypeToSchemaSet(Type type, XmlSchemaSet schemaSet, ISerializationManager serializationManager)
        {
            var attribute = (from x in type.GetCustomAttributesEx(typeof(XmlSchemaProviderAttribute), false)
                             select x as XmlSchemaProviderAttribute).FirstOrDefault();
            if (attribute == null)
            {
                if (TypeHelper.IsBasicType(type))
                {
                    return new XmlQualifiedName(type.Name.ToLower(), Xmlns);
                }

                return null;
            }

            var methodToInvoke = type.GetMethodEx(attribute.MethodName, BindingFlags.Public | BindingFlags.Static);
            if (methodToInvoke == null)
            {
                Log.Error("Expected method '{0}.{1}' because of the XmlSchemaProvider attribute, but it was not found", type.FullName, attribute.MethodName);
                return null;
            }

            var qualifiedName = (XmlQualifiedName)methodToInvoke.Invoke(null, new object[] { schemaSet });
            return qualifiedName;
        }
 private static MethodInfo GetRegisterSimplePropertyMethodInfo(Type modelBaseType)
 {
     return _registerSimplePropertyCache.GetFromCacheOrFetch(modelBaseType, () =>
     {
         var bindingFlags = BindingFlags.FlattenHierarchy | BindingFlags.Instance | BindingFlags.NonPublic;
         var methodInfo = modelBaseType.GetMethodEx("RegisterDynamicProperty", bindingFlags);
         return methodInfo;
     });
 }
        private static bool IsCompatible(Type parameterType, IExpressionNode node)
        {
            var lambdaExpressionNode = node as ILambdaExpressionNode;
            if (lambdaExpressionNode == null)
                return true;

            if (typeof(Expression).IsAssignableFrom(parameterType) && parameterType.IsGenericType())
                parameterType = parameterType.GetGenericArguments()[0];
            if (!typeof(Delegate).IsAssignableFrom(parameterType))
                return false;

            var method = parameterType.GetMethodEx("Invoke", MemberFlags.Public | MemberFlags.Instance);
            if (method == null || method.GetParameters().Length != lambdaExpressionNode.Parameters.Count)
                return false;
            return true;
        }