Esempio n. 1
0
        /// <summary>
        /// Registers a type
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="accessMode">The access mode (used only if a default type descriptor is created).</param>
        /// <param name="friendlyName">Friendly name of the descriptor.</param>
        /// <param name="descriptor">The descriptor, or null to use a default one.</param>
        /// <returns></returns>
        internal static IUserDataDescriptor RegisterType_Impl(Type type, InteropAccessMode accessMode, string friendlyName, IUserDataDescriptor descriptor)
        {
            accessMode = ResolveDefaultAccessModeForType(accessMode, type);

            lock (s_Lock)
            {
                IUserDataDescriptor oldDescriptor = null;
                s_TypeRegistry.TryGetValue(type, out oldDescriptor);

                if (descriptor == null)
                {
                    if (IsTypeBlacklisted(type))
                    {
                        return(null);
                    }

                    if (Framework.Do.GetInterfaces(type).Any(ii => ii == typeof(IUserDataType)))
                    {
                        AutoDescribingUserDataDescriptor audd = new AutoDescribingUserDataDescriptor(type, friendlyName);
                        return(PerformRegistration(type, audd, oldDescriptor));
                    }
                    else if (Framework.Do.IsGenericTypeDefinition(type))
                    {
                        StandardGenericsUserDataDescriptor typeGen = new StandardGenericsUserDataDescriptor(type, accessMode);
                        return(PerformRegistration(type, typeGen, oldDescriptor));
                    }
                    else if (Framework.Do.IsEnum(type))
                    {
                        var enumDescr = new StandardEnumUserDataDescriptor(type, friendlyName);
                        return(PerformRegistration(type, enumDescr, oldDescriptor));
                    }
                    else
                    {
                        StandardUserDataDescriptor udd = new StandardUserDataDescriptor(type, accessMode, friendlyName);

                        if (accessMode == InteropAccessMode.BackgroundOptimized)
                        {
#if NETFX_CORE
                            System.Threading.Tasks.Task.Run(() => ((IOptimizableDescriptor)udd).Optimize());
#else
                            ThreadPool.QueueUserWorkItem(o => ((IOptimizableDescriptor)udd).Optimize());
#endif
                        }

                        return(PerformRegistration(type, udd, oldDescriptor));
                    }
                }
                else
                {
                    PerformRegistration(type, descriptor, oldDescriptor);
                    return(descriptor);
                }
            }
        }
		/// <summary>
		/// Registers a type
		/// </summary>
		/// <param name="type">The type.</param>
		/// <param name="accessMode">The access mode (used only if a default type descriptor is created).</param>
		/// <param name="friendlyName">Friendly name of the descriptor.</param>
		/// <param name="descriptor">The descriptor, or null to use a default one.</param>
		/// <returns></returns>
		internal static IUserDataDescriptor RegisterType_Impl(Type type, InteropAccessMode accessMode, string friendlyName, IUserDataDescriptor descriptor)
		{
			accessMode = ResolveDefaultAccessModeForType(accessMode, type);

			lock (s_Lock)
			{
				IUserDataDescriptor oldDescriptor = null;
				s_TypeRegistry.TryGetValue(type, out oldDescriptor);

				if (descriptor == null)
				{
					if (IsTypeBlacklisted(type))
						return null;

					if (Framework.Do.GetInterfaces(type).Any(ii => ii == typeof(IUserDataType)))
					{
						AutoDescribingUserDataDescriptor audd = new AutoDescribingUserDataDescriptor(type, friendlyName);
						return PerformRegistration(type, audd, oldDescriptor);
					}
					else if (Framework.Do.IsGenericTypeDefinition(type))
					{
						StandardGenericsUserDataDescriptor typeGen = new StandardGenericsUserDataDescriptor(type, accessMode);
						return PerformRegistration(type, typeGen, oldDescriptor);
					}
					else if (Framework.Do.IsEnum(type))
					{
						var enumDescr = new StandardEnumUserDataDescriptor(type, friendlyName);
						return PerformRegistration(type, enumDescr, oldDescriptor);
					}
					else
					{
						StandardUserDataDescriptor udd = new StandardUserDataDescriptor(type, accessMode, friendlyName);

						if (accessMode == InteropAccessMode.BackgroundOptimized)
						{
#if NETFX_CORE
							System.Threading.Tasks.Task.Run(() => ((IOptimizableDescriptor)udd).Optimize());
#else
							ThreadPool.QueueUserWorkItem(o => ((IOptimizableDescriptor)udd).Optimize());
#endif
						}

						return PerformRegistration(type, udd, oldDescriptor);
					}
				}
				else
				{
					PerformRegistration(type, descriptor, oldDescriptor);
					return descriptor;
				}
			}
		}
        /// <summary>
        ///     Registers a type
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="accessMode">The access mode (used only if a default type descriptor is created).</param>
        /// <param name="friendlyName">Friendly name of the descriptor.</param>
        /// <param name="descriptor">The descriptor, or null to use a default one.</param>
        /// <returns></returns>
        internal static IUserDataDescriptor RegisterType_Impl(Type type, InteropAccessMode accessMode,
            string friendlyName, IUserDataDescriptor descriptor)
        {
            if (accessMode == InteropAccessMode.Default)
            {
                var attr = type.GetCustomAttributes(true).OfType<MoonSharpUserDataAttribute>()
                    .SingleOrDefault();

                if (attr != null)
                    accessMode = attr.AccessMode;
            }


            if (accessMode == InteropAccessMode.Default)
                accessMode = s_DefaultAccessMode;

            lock (s_Lock)
            {
                if (!s_Registry.ContainsKey(type))
                {
                    if (descriptor == null)
                    {
                        if (IsTypeBlacklisted(type))
                            return null;

                        if (type.GetInterfaces().Any(ii => ii == typeof (IUserDataType)))
                        {
                            var audd = new AutoDescribingUserDataDescriptor(type, friendlyName);
                            s_Registry.Add(type, audd);
                            return audd;
                        }
                        if (type.IsGenericTypeDefinition)
                        {
                            var typeGen = new StandardGenericsUserDataDescriptor(type, accessMode);
                            s_Registry.Add(type, typeGen);
                            return typeGen;
                        }
                        if (type.IsEnum)
                        {
                            var enumDescr = new StandardEnumUserDataDescriptor(type, friendlyName);
                            s_Registry.Add(type, enumDescr);
                            return enumDescr;
                        }
                        var udd = new StandardUserDataDescriptor(type, accessMode, friendlyName);
                        s_Registry.Add(type, udd);

                        if (accessMode == InteropAccessMode.BackgroundOptimized)
                        {
                            ThreadPool.QueueUserWorkItem(o => ((IOptimizableDescriptor) udd).Optimize());
                        }

                        return udd;
                    }
                    s_Registry.Add(type, descriptor);
                    return descriptor;
                }
                return s_Registry[type];
            }
        }
Esempio n. 4
0
        /// <summary>
        /// Registers a type
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="accessMode">The access mode (used only if a default type descriptor is created).</param>
        /// <param name="friendlyName">Friendly name of the descriptor.</param>
        /// <param name="descriptor">The descriptor, or null to use a default one.</param>
        /// <returns></returns>
        internal static IUserDataDescriptor RegisterType_Impl(Type type, InteropAccessMode accessMode, string friendlyName, IUserDataDescriptor descriptor)
        {
            if (accessMode == InteropAccessMode.Default)
            {
                MoonSharpUserDataAttribute attr = type.GetCustomAttributes(true).OfType <MoonSharpUserDataAttribute>()
                                                  .SingleOrDefault();

                if (attr != null)
                {
                    accessMode = attr.AccessMode;
                }
            }


            if (accessMode == InteropAccessMode.Default)
            {
                accessMode = s_DefaultAccessMode;
            }

            lock (s_Lock)
            {
                if (!s_Registry.ContainsKey(type))
                {
                    if (descriptor == null)
                    {
                        if (IsTypeBlacklisted(type))
                        {
                            return(null);
                        }

                        if (type.GetInterfaces().Any(ii => ii == typeof(IUserDataType)))
                        {
                            AutoDescribingUserDataDescriptor audd = new AutoDescribingUserDataDescriptor(type, friendlyName);
                            s_Registry.Add(type, audd);
                            return(audd);
                        }
                        else if (type.IsGenericTypeDefinition)
                        {
                            StandardGenericsUserDataDescriptor typeGen = new StandardGenericsUserDataDescriptor(type, accessMode);
                            s_Registry.Add(type, typeGen);
                            return(typeGen);
                        }
                        else if (type.IsEnum)
                        {
                            var enumDescr = new StandardEnumUserDataDescriptor(type, friendlyName);
                            s_Registry.Add(type, enumDescr);
                            return(enumDescr);
                        }
                        else
                        {
                            StandardUserDataDescriptor udd = new StandardUserDataDescriptor(type, accessMode, friendlyName);
                            s_Registry.Add(type, udd);

                            if (accessMode == InteropAccessMode.BackgroundOptimized)
                            {
                                ThreadPool.QueueUserWorkItem(o => ((IOptimizableDescriptor)udd).Optimize());
                            }

                            return(udd);
                        }
                    }
                    else
                    {
                        s_Registry.Add(type, descriptor);
                        return(descriptor);
                    }
                }
                else
                {
                    return(s_Registry[type]);
                }
            }
        }