コード例 #1
0
        /// <summary>
        /// Removes an alias from the global list of aliases
        /// </summary>
        /// <param name="Name">The name of the laias to kill</param>
        /// <param name="Force">Whether to remove ReadOnly items</param>
        public static void RemovePowerShellAlias(string Name, bool Force)
        {
            object context       = GetExecutionContextFromTLS();
            object topLevelState = GetPrivateProperty("TopLevelSessionState", context);
            object globalScope   = GetPrivateProperty("GlobalScope", topLevelState);
            Dictionary <string, AliasInfo> aliases = (Dictionary <string, AliasInfo>)GetPrivateField("_alias", globalScope);

            if (!aliases.ContainsKey(Name))
            {
                throw new ItemNotFoundException(String.Format(LocalizationHost.Read("PSFramework.Assembly.UtilityHost.AliasNotFound"), Name));
            }

            AliasInfo alias = aliases[Name];

            if ((alias.Options & ScopedItemOptions.Constant) != 0)
            {
                throw new InvalidOperationException(String.Format(LocalizationHost.Read("PSFramework.Assembly.UtilityHost.AliasProtected"), Name));
            }
            if (!Force && ((alias.Options & ScopedItemOptions.ReadOnly) != 0))
            {
                throw new InvalidOperationException(String.Format(LocalizationHost.Read("PSFramework.Assembly.UtilityHost.AliasReadOnly"), Name));
            }

            aliases.Remove(Name);
        }
コード例 #2
0
        /// <summary>
        /// Returns a static private method of a type
        /// </summary>
        /// <param name="StaticType">The type to search in</param>
        /// <param name="Name">The name of the method to retrieve</param>
        /// <returns>The method object requested</returns>
        public static MethodInfo GetPrivateStaticMethod(Type StaticType, string Name)
        {
            MethodInfo method = StaticType.GetMethod(Name, BindingFlags.Static | BindingFlags.NonPublic);

            if (method == null)
            {
                throw new ArgumentException(LocalizationHost.Read(String.Format("PSFramework.Assembly.UtilityHost.PrivateMethodNotFound", Name)), "Name");
            }
            return(method);
        }
コード例 #3
0
        /// <summary>
        /// Returns the value of a private static property on a type
        /// </summary>
        /// <param name="StaticType">The type to pick from</param>
        /// <param name="Name">The name of the property to retrieve</param>
        /// <returns>The value of the property content (may be null)</returns>
        public static object GetPrivateStaticProperty(Type StaticType, string Name)
        {
            PropertyInfo property = StaticType.GetProperty(Name, BindingFlags.Static | BindingFlags.NonPublic);

            if (property == null)
            {
                throw new ArgumentException(LocalizationHost.Read(String.Format("PSFramework.Assembly.UtilityHost.PrivatePropertyNotFound", Name)), "Name");
            }
            return(property.GetValue(null, BindingFlags.NonPublic | BindingFlags.Static, null, null, System.Globalization.CultureInfo.CurrentCulture));
        }
コード例 #4
0
        /// <summary>
        /// Returns the value of a private field on an object
        /// </summary>
        /// <param name="Name">The name of the field</param>
        /// <param name="Instance">The object from which to read the field from</param>
        /// <returns>The value of the field content (may be null)</returns>
        public static object GetPrivateField(string Name, object Instance)
        {
            if (Instance == null)
            {
                return(null);
            }

            FieldInfo property = Instance.GetType().GetField(Name, BindingFlags.Instance | BindingFlags.NonPublic);

            if (property == null)
            {
                throw new ArgumentException(LocalizationHost.Read(String.Format("PSFramework.Assembly.UtilityHost.PrivateFieldNotFound", Name)), "Name");
            }
            return(property.GetValue(Instance));
        }
コード例 #5
0
        /// <summary>
        /// Updates the value of a private property
        /// </summary>
        /// <param name="Name">The name of the property to update</param>
        /// <param name="Instance">The object that contains the property to update</param>
        /// <param name="Value">The value to apply</param>
        public static void SetPrivateProperty(string Name, object Instance, object Value)
        {
            if (Instance == null)
            {
                return;
            }

            PropertyInfo property = Instance.GetType().GetProperty(Name, BindingFlags.Instance | BindingFlags.NonPublic);

            if (property == null)
            {
                throw new ArgumentException(String.Format(LocalizationHost.Read("PSFramework.Assembly.UtilityHost.PrivatePropertyNotFound"), Name), "Name");
            }

            property.SetValue(Instance, Value);
        }