예제 #1
0
        public DynamicParameterInstance EnableParameter(
            [NotNull] DynamicParameterDescriptor descriptor)
        {
            if (descriptor is null)
            {
                throw new ArgumentNullException(nameof(descriptor));
            }

            var instance = new DynamicParameterInstance(this, descriptor);

            this._enabledParameters.Add(descriptor.ParameterName, instance);

            return(instance);
        }
        internal DynamicParameterInstance(
            [NotNull] DynamicParameterContext context,
            [NotNull] DynamicParameterDescriptor descriptor)
        {
            if (context is null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            if (descriptor is null)
            {
                throw new ArgumentNullException(nameof(descriptor));
            }

            this.Context             = context;
            this.ParameterDescriptor = descriptor;
        }
        private static void AddParameter(
            [NotNull] TypeBuilder typeBuilder,
            [NotNull] DynamicParameterDescriptor descriptor,
            [NotNull] FieldInfo contextField)
        {
            string parameterName = descriptor.ParameterName;
            var    parameterType = descriptor.ParameterType;

            var propertyBuilder = typeBuilder.DefineProperty(
                parameterName,
                PropertyAttributes.None,
                parameterType,
                Type.EmptyTypes);

            SetParameterAttributes(propertyBuilder, descriptor, true);

            const MethodAttributes accessorAttributes =
                MethodAttributes.Public |
                MethodAttributes.PrivateScope |
                MethodAttributes.HideBySig |
                MethodAttributes.SpecialName;

            var getterBuilder = typeBuilder.DefineMethod(
                "get_" + parameterName,
                accessorAttributes,
                parameterType,
                Type.EmptyTypes);

            var getterGenerator = getterBuilder.GetILGenerator();

            EmitLoadMember(getterGenerator, descriptor, contextField);

            var setterBuilder = typeBuilder.DefineMethod(
                "set_" + parameterName,
                accessorAttributes,
                typeof(void),
                new[] { parameterType });

            var setterGenerator = setterBuilder.GetILGenerator();

            EmitStoreMember(setterGenerator, descriptor, contextField);

            propertyBuilder.SetGetMethod(getterBuilder);
            propertyBuilder.SetSetMethod(setterBuilder);
        }
예제 #4
0
        public static DynamicParameterInstance EnableDynamicParameter <TCmdlet>(
            [NotNull] this TCmdlet cmdlet,
            [NotNull] DynamicParameterDescriptor parameterDescriptor)
            where TCmdlet : Cmdlet
        {
            if (cmdlet is null)
            {
                throw new ArgumentNullException(nameof(cmdlet));
            }

            if (parameterDescriptor is null)
            {
                throw new ArgumentNullException(nameof(parameterDescriptor));
            }

            var context  = DynamicParameterContext.GetContext(cmdlet);
            var instance = context.EnableParameter(parameterDescriptor);

            return(instance);
        }
        private static void SetParameterAttributes(
            [NotNull] PropertyBuilder propertyBuilder,
            [NotNull] DynamicParameterDescriptor parameterDescriptor,
            bool checkAttributeTarget)
        {
            foreach (var attribute in parameterDescriptor.Attributes)
            {
                if (checkAttributeTarget)
                {
                    bool valid = IsAttributeValidOnProperty(attribute);
                    if (!valid)
                    {
                        continue;
                    }
                }

                var attributeBuilder = ConvertToCustomAttributeBuilder(attribute);
                propertyBuilder.SetCustomAttribute(attributeBuilder);
            }
        }
        private static void EmitLoadMember(
            [NotNull] ILGenerator generator,
            [NotNull] DynamicParameterDescriptor descriptor,
            [NotNull] FieldInfo contextField)
        {
            var method = typeof(DynamicParameterContext).GetMethod(
                nameof(DynamicParameterContext.GetParameterValue),
                BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);

            generator.Emit(OpCodes.Ldarg_0);
            generator.Emit(OpCodes.Ldfld, contextField);

            generator.Emit(OpCodes.Ldstr, descriptor.ParameterName);
            generator.EmitCall(OpCodes.Call, method, Type.EmptyTypes);

            if (descriptor.ParameterType.IsValueType)
            {
                generator.Emit(OpCodes.Unbox_Any, descriptor.ParameterType);
            }

            generator.Emit(OpCodes.Ret);
        }
 public void SetValue(
     IDynamicParameterContext context,
     DynamicParameterDescriptor descriptor,
     ref object value)
 {
 }