/// <summary>
		/// Registers an extension Type (that is a type containing extension methods)
		/// </summary>
		/// <param name="type">The type.</param>
		/// <param name="mode">The InteropAccessMode.</param>
		public static void RegisterExtensionType(Type type, InteropAccessMode mode = InteropAccessMode.Default)
		{
			lock (s_Lock)
			{
				bool changesDone = false;

				foreach (MethodInfo mi in type.GetMethods().Where(_mi => _mi.IsStatic))
				{
					if (mi.GetCustomAttributes(typeof(ExtensionAttribute), false).Length == 0)
						continue;

					if (mi.ContainsGenericParameters)
					{
						s_UnresolvedGenericsRegistry.Add(mi.Name, new UnresolvedGenericMethod(mi, mode));
						changesDone = true;
						continue;
					}

					if (!MethodMemberDescriptor.CheckMethodIsCompatible(mi, false))
						continue;

					var desc = new MethodMemberDescriptor(mi, mode);

					s_Registry.Add(mi.Name, desc);
					changesDone = true;
				}

				if (changesDone)
					++s_ExtensionMethodChangeVersion;
			}
		}
        /// <summary>
        /// Creates a CallbackFunction from a MethodInfo relative to a function.
        /// </summary>
        /// <param name="script">The script.</param>
        /// <param name="mi">The MethodInfo object.</param>
        /// <param name="obj">The object to which the function applies, or null for static methods.</param>
        /// <param name="accessMode">The access mode.</param>
        /// <returns></returns>
        /// <exception cref="System.ArgumentException">The method is not static.</exception>
        public static CallbackFunction FromMethodInfo(Script script, System.Reflection.MethodInfo mi, object obj = null, InteropAccessMode accessMode = InteropAccessMode.Default)
        {
            if (accessMode == InteropAccessMode.Default)
            {
                accessMode = m_DefaultAccessMode;
            }

            MethodMemberDescriptor descr = new MethodMemberDescriptor(mi, accessMode);

            return(descr.GetCallbackFunction(script, obj));
        }
Exemplo n.º 3
0
        /// <summary>
        /// Creates a CallbackFunction from a delegate.
        /// </summary>
        /// <param name="script">The script.</param>
        /// <param name="del">The delegate.</param>
        /// <param name="accessMode">The access mode.</param>
        /// <returns></returns>
        public static CallbackFunction FromDelegate(Script script, Delegate del, InteropAccessMode accessMode = InteropAccessMode.Default)
        {
            if (accessMode == InteropAccessMode.Default)
            {
                accessMode = m_DefaultAccessMode;
            }

            MethodMemberDescriptor descr = new MethodMemberDescriptor(del.Method, accessMode);

            return(descr.GetCallbackFunction(script, del.Target));
        }
        /// <summary>
        /// Creates a CallbackFunction from a delegate.
        /// </summary>
        /// <param name="script">The script.</param>
        /// <param name="del">The delegate.</param>
        /// <param name="accessMode">The access mode.</param>
        /// <returns></returns>
        public static CallbackFunction FromDelegate(Script script, Delegate del, InteropAccessMode accessMode = InteropAccessMode.Default)
        {
            if (accessMode == InteropAccessMode.Default)
            {
                accessMode = m_DefaultAccessMode;
            }

#if NETFX_CORE
            MethodMemberDescriptor descr = new MethodMemberDescriptor(del.GetMethodInfo(), accessMode);
#else
            MethodMemberDescriptor descr = new MethodMemberDescriptor(del.Method, accessMode);
#endif
            return(descr.GetCallbackFunction(script, del.Target));
        }
Exemplo n.º 5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Script"/> class.
        /// </summary>
        /// <param name="coreModules">The core modules to be pre-registered in the default global table.</param>
        public Script(CoreModules coreModules)
        {
            Options          = new ScriptOptions(DefaultOptions);
            PerformanceStats = new PerformanceStatistics();
            Registry         = new Table(this);

            m_ByteCode      = new ByteCode(this);
            m_MainProcessor = new Processor(this, m_GlobalTable, m_ByteCode);
            m_GlobalTable   = new Table(this).RegisterCoreModules(coreModules);
            foreach (var t in TypeDescriptorRegistry.RegisteredTypes)
            {
                Type valType = t.Value.Type;
                if (valType.GetTypeInfo().Assembly.FullName != typeof(Script).GetTypeInfo().Assembly.FullName)
                {
                    // m_GlobalTable.RegisterModuleType(t.Value.Type);
                    if (t.Value is StandardUserDataDescriptor)
                    {
                        StandardUserDataDescriptor desc = (StandardUserDataDescriptor)t.Value;
                        foreach (var member in desc.Members)
                        {
                            if (member.Value is MethodMemberDescriptor)
                            {
                                MethodMemberDescriptor methDesc = (MethodMemberDescriptor)member.Value;
                                if (methDesc.IsConstructor)
                                {
                                    m_GlobalTable.Set(methDesc.Name, methDesc.GetCallbackAsDynValue(this));
                                }
                            }
                            else if (member.Value is OverloadedMethodMemberDescriptor)
                            {
                                OverloadedMethodMemberDescriptor methDesc = (OverloadedMethodMemberDescriptor)member.Value;
                                foreach (var overloadDesc in methDesc.m_Overloads)
                                {
                                    if (overloadDesc is MethodMemberDescriptor)
                                    {
                                        MethodMemberDescriptor actualDesc = (MethodMemberDescriptor)overloadDesc;
                                        if (actualDesc.IsConstructor)
                                        {
                                            //m_GlobalTable.Set(desc.FriendlyName, actualDesc.GetCallbackAsDynValue(this));
                                            m_GlobalTable.Set(desc.FriendlyName, DynValue.NewCallback(methDesc.GetCallbackFunction(this)));
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Gets all the extension methods which can match a given name and extending a given Type
        /// </summary>
        /// <param name="name">The name.</param>
        /// <param name="extendedType">The extended type.</param>
        public static List <IOverloadableMemberDescriptor> GetExtensionMethodsByNameAndType(string name,
                                                                                            Type extendedType)
        {
            List <UnresolvedGenericMethod> unresolvedGenerics;

            lock (s_Lock)
            {
                unresolvedGenerics = s_UnresolvedGenericsRegistry.Find(name).ToList();
            }

            foreach (var ugm in unresolvedGenerics)
            {
                var args = ugm.Method.GetParameters();
                if (args.Length == 0)
                {
                    continue;
                }

                var extensionType = args[0].ParameterType;

                var genericType = GetGenericMatch(extensionType, extendedType);

                if (ugm.AlreadyAddedTypes.Add(genericType))
                {
                    if (genericType != null)
                    {
                        var mi = InstantiateMethodInfo(ugm.Method, extensionType, genericType, extendedType);
                        if (mi != null)
                        {
                            if (!MethodMemberDescriptor.CheckMethodIsCompatible(mi, false))
                            {
                                continue;
                            }

                            var desc = new MethodMemberDescriptor(mi, ugm.AccessMode);

                            s_Registry.Add(ugm.Method.Name, desc);
                            ++s_ExtensionMethodChangeVersion;
                        }
                    }
                }
            }

            return(s_Registry.Find(name)
                   .Where(d => d.ExtensionMethodType != null &&
                          Framework.Do.IsAssignableFrom(d.ExtensionMethodType, extendedType))
                   .ToList());
        }
Exemplo n.º 7
0
        /// <summary>
        /// Registers an extension Type (that is a type containing extension methods)
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="mode">The InteropAccessMode.</param>
        public static void RegisterExtensionType(Type type, InteropAccessMode mode = InteropAccessMode.Default)
        {
            lock (s_Lock)
            {
                bool changesDone = false;

                foreach (MethodInfo mi in type.GetMethods().Where(_mi => _mi.IsStatic))
                {
                    if (mi.GetCustomAttributes(typeof(ExtensionAttribute), false).Length == 0)
                    {
                        continue;
                    }

                    if (mi.ContainsGenericParameters)
                    {
                        s_UnresolvedGenericsRegistry.Add(mi.Name, new UnresolvedGenericMethod(mi, mode));
                        changesDone = true;
                        continue;
                    }

                    if (!MethodMemberDescriptor.CheckMethodIsCompatible(mi, false))
                    {
                        continue;
                    }

                    var desc = new MethodMemberDescriptor(mi, mode);

                    s_Registry.Add(mi.Name, desc);
                    changesDone = true;
                }

                if (changesDone)
                {
                    ++s_ExtensionMethodChangeVersion;
                }
            }
        }
		/// <summary>
		/// Gets all the extension methods which can match a given name and extending a given Type
		/// </summary>
		/// <param name="name">The name.</param>
		/// <param name="extendedType">The extended type.</param>
		/// <returns></returns>
		public static List<IOverloadableMemberDescriptor> GetExtensionMethodsByNameAndType(string name, Type extendedType)
		{
			List<UnresolvedGenericMethod> unresolvedGenerics = null;

			lock (s_Lock)
			{
				unresolvedGenerics = s_UnresolvedGenericsRegistry.Find(name).ToList();
			}

			foreach (UnresolvedGenericMethod ugm in unresolvedGenerics)
			{
				ParameterInfo[] args = ugm.Method.GetParameters();
				if (args.Length == 0) continue;
				Type extensionType = args[0].ParameterType;

				Type genericType = GetGenericMatch(extensionType, extendedType);

				if (ugm.AlreadyAddedTypes.Add(genericType))
				{
					if (genericType != null)
					{
						MethodInfo mi = InstantiateMethodInfo(ugm.Method, extensionType, genericType, extendedType);
						if (mi != null)
						{
							if (!MethodMemberDescriptor.CheckMethodIsCompatible(mi, false))
								continue;

							var desc = new MethodMemberDescriptor(mi, ugm.AccessMode);

							s_Registry.Add(ugm.Method.Name, desc);
							++s_ExtensionMethodChangeVersion;
						}
					}
				}
			}

			return s_Registry.Find(name)
				.Where(d => d.ExtensionMethodType != null && d.ExtensionMethodType.IsAssignableFrom(extendedType))
				.ToList();
		}