コード例 #1
0
        internal IEnumerable <string> PopulateScriptReferences(RustScript rustScript)
        {
            var type = rustScript.GetType();

            var fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

            foreach (var field in fields)
            {
                if (field.FieldType != typeof(IScriptReference) || field.IsSpecialName)
                {
                    continue;
                }

                var scriptName = field.Name.Trim(NameTrimChars);
                if (!_scripts.TryGetValue(scriptName, out var script))
                {
                    continue;
                }

                field.SetValue(rustScript, script);
                yield return(script.Name);
            }

            var properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

            foreach (var property in properties)
            {
                if (property.PropertyType != typeof(IScriptReference) || !property.CanWrite)
                {
                    continue;
                }

                var scriptName = property.Name.Trim(NameTrimChars);
                if (!_scripts.TryGetValue(scriptName, out var script))
                {
                    continue;
                }

                property.SetValue(rustScript, script);
                yield return(script.Name);
            }
        }
コード例 #2
0
        public static T GetInvoker <T>(RustScript instance, string methodName) where T : Delegate
        {
            var objectType   = instance.GetType();
            var delegateType = typeof(T);

            var signature = delegateType.GetMethod("Invoke", BindingFlags.Public | BindingFlags.Instance);

            if (signature == null)
            {
                return(null);
            }

            var parameterTypes = signature.GetParameters().Select(p => p.ParameterType).ToArray();
            var method         = objectType.GetMethod(methodName, BindingFlags.Public | BindingFlags.Instance, null, parameterTypes, null);

            if (method == null || method.ReturnType != signature.ReturnType || method.DeclaringType == typeof(RustScript))
            {
                return(null);
            }

            return((T)method.CreateDelegate(delegateType, instance));
        }