Ejemplo n.º 1
0
        public GenericInterfaceAopProxy()
        {
            _classGenericArgumentT = GenericArgumentInfo.Create(
                typeof(T),
                nameof(T),
                ClassGenericArgumentTIndex,
                Variance.Covariant,
                GenericSource.Class,
                new[]
            {
                GenericArgumentRestriction.Create(GenericArgumentRestrictionType.Class),
                GenericArgumentRestriction.Create(typeof(IDisposable)),
                GenericArgumentRestriction.Create(GenericArgumentRestrictionType.New)
            });

            _classGenericArgumentTa = GenericArgumentInfo.Create(
                typeof(TA),
                nameof(TA),
                ClassGenericArgumentTaIndex,
                Variance.Covariant,
                GenericSource.Class,
                new[]
            {
                GenericArgumentRestriction.Create(GenericArgumentRestrictionType.Struct)
            });

            _classGenericArguments = new[]
            {
                _classGenericArgumentT,
                _classGenericArgumentTa
            };
        }
        public T GetBar <T>() where T : struct
        {
            var genericArgumentT = GenericArgumentInfo.Create(
                typeof(T),
                nameof(T),
                new[] { GenericArgumentRestriction.Create(GenericArgumentRestrictionType.Struct) });

            var invocationInfo = InvocationInfo.Create(
                typeof(InterfaceWithGenericMethodAopProxy),
                nameof(GetBar),
                new[]
            {
                genericArgumentT
            },
                ResultInfo.Create(typeof(T), genericArgumentT),
                GetBar <T>);

            var result = _interceptionHandler.Invoke(invocationInfo);

            return((T)result.Result.Value);
        }
        public void SetBoo <T, TA>(T t, TA a)
            where T : class, IDisposable, new()
            where TA : new()
        {
            var genericArgumentT = GenericArgumentInfo.Create(
                typeof(T),
                nameof(T),
                new[]
            {
                GenericArgumentRestriction.Create(GenericArgumentRestrictionType.Class),
                GenericArgumentRestriction.Create(typeof(IDisposable)),
                GenericArgumentRestriction.Create(GenericArgumentRestrictionType.New)
            });

            var genericArgumentTa = GenericArgumentInfo.Create(
                typeof(TA),
                nameof(TA),
                new[] { GenericArgumentRestriction.Create(GenericArgumentRestrictionType.New) });

            var invocationInfo = InvocationInfo.Create(
                typeof(InterfaceWithGenericMethodAopProxy),
                nameof(GetBar),
                new[]
            {
                ArgumentInfo.Create(typeof(T), nameof(t), t, genericArgumentT),
                ArgumentInfo.Create(typeof(TA), nameof(a), a, genericArgumentTa),
            },
                new[]
            {
                genericArgumentT,
                genericArgumentTa
            },
                SetBoo <T, TA>);

            _interceptionHandler.Invoke(invocationInfo);
        }