示例#1
0
 public void AddGlobalObject(string path, object o)
 {
     if (!(o.GetType() == typeof(string) || o.GetType().IsPrimitive))
     {
         RegisterUserDataType(o.GetType());
     }
     BindingHelpers.CreateBindUserObject(bindItems, path, o);
     //globalObjects[name]
 }
示例#2
0
        private void RegisterAssemblyTypes(Assembly assembly)
        {
            var types = assembly.GetTypes().Where(x => x.GetCustomAttribute <LunarBindHideAttribute>() == null && x.GetCustomAttribute <LunarBindIgnoreAssemblyAddAttribute>() == null);

            foreach (var type in types)
            {
                if (type.IsEnum)
                {
                    var enumAttr = (LunarBindEnumAttribute)type.GetCustomAttribute(typeof(LunarBindEnumAttribute));
                    if (enumAttr != null)
                    {
                        BindingHelpers.CreateBindEnum(bindItems, enumAttr.Name ?? type.Name, type);
                    }
                }
                else
                {
                    var instantiable = (LunarBindInstanceAttribute)type.GetCustomAttribute(typeof(LunarBindInstanceAttribute));
                    if (instantiable != null)
                    {
                        var constructor = type.GetConstructor(new Type[] { });
                        if (constructor != null)
                        {
                            object instance = constructor.Invoke(new object[] { });
                            RegisterUserDataType(type);
                            var bindObj = BindingHelpers.CreateBindUserObject(bindItems, instantiable.Path, instance);
                            var doc     = type.GetCustomAttribute <LunarBindDocumentationAttribute>()?.Data ?? "";
                            var ex      = type.GetCustomAttribute <LunarBindExampleAttribute>()?.Data ?? "";
                            bindObj.Documentation = doc;
                            bindObj.Example       = ex;


                            //AddGlobalObject(instantiable.Path, instance);
                        }
                        else
                        {
                            //TODO: custom exception
                            throw new Exception($"LunarBind: No public empty constructor found on Type [{type.Name}] with LunarBindInstantiableAttribute");
                        }
                    }

                    var staticAttribute = (LunarBindStaticAttribute)type.GetCustomAttribute(typeof(LunarBindStaticAttribute));
                    if (staticAttribute != null)
                    {
                        RegisterUserDataType(type);
                        BindingHelpers.CreateBindType(bindItems, staticAttribute.Path ?? type.Name, type);
                    }

                    RegisterTypeFuncs(type);
                }
            }
        }
 /// <summary>
 /// Add an object at the specified path
 /// </summary>
 /// <param name="path"></param>
 /// <param name="o"></param>
 public static void AddGlobalObject(string path, object o)
 {
     RegisterUserDataType(o.GetType());
     BindingHelpers.CreateBindUserObject(bindItems, path, o);
 }