Exemplo n.º 1
0
        internal CILParameterImpl(
            CILReflectionContextImpl ctx,
            Int32 anID,
            System.Reflection.ParameterInfo parameter
            )
            : base(ctx, anID, CILElementKind.Parameter, () => new CustomAttributeDataEventArgs(ctx, parameter))
        {
            var member = parameter.Member;
            var isCtor = member is System.Reflection.ConstructorInfo;

            InitFields(
                ref this.paramAttributes,
                ref this.position,
                ref this.name,
                ref this.method,
                ref this.parameterType,
                ref this.defaultValue,
                ref this.customModifiers,
                ref this.marshalInfo,
                new SettableValueForEnums <ParameterAttributes>((ParameterAttributes)parameter.Attributes),
                parameter.Position,
                new SettableValueForClasses <String>(parameter.Name),
                () => isCtor ? (CILMethodBase)ctx.Cache.GetOrAdd((System.Reflection.ConstructorInfo)member) : ctx.Cache.GetOrAdd((System.Reflection.MethodInfo)member),
                () => ctx.Cache.GetOrAdd(parameter.ParameterType),
                new SettableLazy <Object>(() => ctx.LaunchConstantValueLoadEvent(new ConstantValueLoadArgs(parameter))),
                ctx.LaunchEventAndCreateCustomModifiers(new CustomModifierEventLoadArgs(parameter)),
                new SettableLazy <MarshalingInfo>(() => MarshalingInfo.FromAttribute(parameter.GetCustomAttributes(true).OfType <System.Runtime.InteropServices.MarshalAsAttribute>().FirstOrDefault(), ctx)),
                true
                );
        }
Exemplo n.º 2
0
        public static bool MatchPattern(this System.Reflection.ParameterInfo parameter, Type customAttribute, string attributeName)
        {
            if (parameter == null)
            {
                return(false);
            }
            var hasCA = parameter.GetCustomAttributes(customAttribute, false).Any();

            if (hasCA)
            {
                return(true);
            }

            return(!string.IsNullOrEmpty(attributeName) && parameter.Name.ToLower().Equals(attributeName.ToLower()));
        }
Exemplo n.º 3
0
 private bool hasParams(System.Reflection.ParameterInfo p)
 {
     return(p.GetCustomAttributes(typeof(System.ParamArrayAttribute), false).Length > 0);
 }
Exemplo n.º 4
0
 /// <summary>
 ///     Retrieves an array of the custom attributes applied to a method parameter. Parameters specify the method
 ///     parameter, and whether to search ancestors of the method parameter.
 /// </summary>
 /// <param name="element">An object derived from the  class that describes a parameter of a member of a class.</param>
 /// <param name="inherit">If true, specifies to also search the ancestors of  for custom attributes.</param>
 /// <returns>
 ///     An  array that contains the custom attributes applied to , or an empty array if no such custom attributes
 ///     exist.
 /// </returns>
 public static Attribute[] GetCustomAttributes(this System.Reflection.ParameterInfo element, Boolean inherit)
 {
     return((Attribute[])element.GetCustomAttributes(inherit));
 }
Exemplo n.º 5
0
 /// <summary>
 ///     Retrieves an array of the custom attributes applied to a method parameter. Parameters specify the method
 ///     parameter, and the type of the custom attribute to search for.
 /// </summary>
 /// <param name="element">An object derived from the  class that describes a parameter of a member of a class.</param>
 /// <param name="attributeType">The type, or a base type, of the custom attribute to search for.</param>
 /// <returns>
 ///     An  array that contains the custom attributes of type  applied to , or an empty array if no such custom
 ///     attributes exist.
 /// </returns>
 public static Attribute[] GetCustomAttributes(this System.Reflection.ParameterInfo element, Type attributeType)
 {
     return(element.GetCustomAttributes(attributeType));
 }
Exemplo n.º 6
0
        public static TypeCache ForParameter(System.Reflection.ParameterInfo parameter, string logicalname)
        {
            var key = new Key()
            {
                Parameter = parameter, LogicalName = logicalname
            };

            var type = parameter.ParameterType;

            if (resolvedTypes.ContainsKey(key))
            {
                return(resolvedTypes[key]);
            }

            lock (locks)
            {
                if (resolvedTypes.ContainsKey(key))
                {
                    return(resolvedTypes[key]);
                }

                if (parameter.ParameterType == typeof(Guid))
                {
                    resolvedTypes[key] = new TypeCache {
                        FromType = type, ToType = type, Name = parameter.Name
                    };
                    return(resolvedTypes[key]);
                }

                if (parameter.ParameterType == typeof(string))
                {
                    resolvedTypes[key] = new TypeCache {
                        FromType = type, ToType = type, Name = parameter.Name, IsInputParameter = true
                    };
                    return(resolvedTypes[key]);
                }

                if (parameter.ParameterType == typeof(int))
                {
                    resolvedTypes[key] = new TypeCache {
                        FromType = type, ToType = type, Name = parameter.Name, IsInputParameter = true
                    };
                    return(resolvedTypes[key]);
                }

                if (parameter.ParameterType == typeof(int?))
                {
                    resolvedTypes[key] = new TypeCache {
                        FromType = type, ToType = type, Name = parameter.Name, IsInputParameter = true
                    };
                    return(resolvedTypes[key]);
                }

                if (parameter.ParameterType == typeof(decimal))
                {
                    resolvedTypes[key] = new TypeCache {
                        FromType = type, ToType = type, Name = parameter.Name, IsInputParameter = true
                    };
                    return(resolvedTypes[key]);
                }

                if (parameter.ParameterType == typeof(decimal?))
                {
                    resolvedTypes[key] = new TypeCache {
                        FromType = type, ToType = type, Name = parameter.Name, IsInputParameter = true
                    };
                    return(resolvedTypes[key]);
                }

                if (parameter.ParameterType == typeof(bool))
                {
                    resolvedTypes[key] = new TypeCache {
                        FromType = type, ToType = type, Name = parameter.Name, IsInputParameter = true
                    };
                    return(resolvedTypes[key]);
                }

                if (parameter.ParameterType == typeof(bool?))
                {
                    resolvedTypes[key] = new TypeCache {
                        FromType = type, ToType = type, Name = parameter.Name, IsInputParameter = true
                    };
                    return(resolvedTypes[key]);
                }

                if (parameter.ParameterType == typeof(Microsoft.Xrm.Sdk.EntityReference))
                {
                    resolvedTypes[key] = new TypeCache {
                        FromType = type, ToType = type, Name = parameter.Name, IsInputParameter = true
                    };
                    return(resolvedTypes[key]);
                }

                if (parameter.ParameterType == typeof(Microsoft.Xrm.Sdk.OptionSetValue))
                {
                    resolvedTypes[key] = new TypeCache {
                        FromType = type, ToType = type, Name = parameter.Name, IsInputParameter = true
                    };
                    return(resolvedTypes[key]);
                }

                if (parameter.ParameterType == typeof(Microsoft.Xrm.Sdk.Money))
                {
                    resolvedTypes[key] = new TypeCache {
                        FromType = type, ToType = type, Name = parameter.Name, IsInputParameter = true
                    };
                    return(resolvedTypes[key]);
                }

                if (parameter.ParameterType == typeof(Microsoft.Xrm.Sdk.IOrganizationService))
                {
                    resolvedTypes[key] = new TypeCache {
                        FromType = type, ToType = type
                    };

                    resolvedTypes[key].RequireAdminService = parameter.GetCustomAttributes(Types.AdminAttribute, false).Any();
                    return(resolvedTypes[key]);
                }

                if (parameter.ParameterType == typeof(Microsoft.Xrm.Sdk.IOrganizationServiceFactory))
                {
                    resolvedTypes[key] = new TypeCache {
                        FromType = type, ToType = type
                    };
                    return(resolvedTypes[key]);
                }

                if (parameter.ParameterType == typeof(Microsoft.Xrm.Sdk.IPluginExecutionContext))
                {
                    resolvedTypes[key] = new TypeCache {
                        FromType = type, ToType = type
                    };
                    return(resolvedTypes[key]);
                }

                if (parameter.ParameterType == typeof(Microsoft.Xrm.Sdk.ITracingService))
                {
                    resolvedTypes[key] = new TypeCache {
                        FromType = type, ToType = type
                    };
                    return(resolvedTypes[key]);
                }

                if (parameter.ParameterType == Types.IPluginContext)
                {
                    resolvedTypes[key] = new TypeCache {
                        FromType = type, ToType = type
                    };
                    return(resolvedTypes[key]);
                }

                if (parameter.ParameterType == typeof(Microsoft.Xrm.Sdk.Query.QueryExpression))
                {
                    resolvedTypes[key] = new TypeCache {
                        FromType = type, Name = parameter.Name, ToType = type
                    };
                    return(resolvedTypes[key]);
                }

                if (parameter.ParameterType == typeof(Microsoft.Xrm.Sdk.Relationship))
                {
                    resolvedTypes[key] = new TypeCache {
                        FromType = type, Name = parameter.Name, ToType = type
                    };
                    return(resolvedTypes[key]);
                }

                if (parameter.Name.ToLower() == "datasource" && (parameter.ParameterType == typeof(Microsoft.Xrm.Sdk.Entity) || parameter.ParameterType.BaseType == typeof(Microsoft.Xrm.Sdk.Entity)))
                {
                    resolvedTypes[key] = new TypeCache {
                        FromType = type, Name = parameter.Name, ToType = type
                    };
                    return(resolvedTypes[key]);
                }

                #region not an abstract, and not an interface, the type can be used directly, see if the name indicates that it is target, preimage, mergedimage or postimage
                if (!type.IsInterface && !type.IsAbstract)
                {
                    var constructors = type.GetConstructors(System.Reflection.BindingFlags.Public | System.Reflection.BindingFlags.Instance);

                    if (constructors != null && constructors.Length > 0)
                    {
                        var result = new TypeCache {
                            FromType = type, ToType = type
                        };
                        var isEntity = false;

                        #region see if we can resolve parameter to the target as an entity
                        if (parameter.MatchPattern(Types.TargetAttribute, "target") && type.BaseType == typeof(Microsoft.Xrm.Sdk.Entity))
                        {
                            isEntity        = true;
                            result.IsTarget = true;
                        }
                        else
                        if (parameter.MatchPattern(Types.PreimageAttribute, "preimage") && type.BaseType == typeof(Microsoft.Xrm.Sdk.Entity))
                        {
                            isEntity          = true;
                            result.IsPreimage = true;
                        }
                        else
                        if (parameter.MatchPattern(Types.MergedimageAttribute, "mergedimage") && type.BaseType == typeof(Microsoft.Xrm.Sdk.Entity))
                        {
                            isEntity             = true;
                            result.IsMergedimage = true;
                        }
                        else
                        if (parameter.MatchPattern(Types.PostimageAttribute, "postimage") && type.BaseType == typeof(Microsoft.Xrm.Sdk.Entity))
                        {
                            isEntity           = true;
                            result.IsPostimage = true;
                        }

                        if (isEntity)
                        {
                            var entity = (Microsoft.Xrm.Sdk.Entity)Activator.CreateInstance(type);
                            result.LogicalName = entity.LogicalName;
                            result.ResolveProperties();
                        }
                        #endregion

                        var isReference = false;

                        #region see if we can resolve parameter to the target as en entity reference
                        if (!isEntity && type.ExtendsGenericClassOf(Types.TargetReference))
                        {
                            isReference        = true;
                            result.IsTarget    = true;
                            result.IsReference = true;
                            result.ToType      = type.BaseType.GetGenericArguments()[0];

                            var entity = (Microsoft.Xrm.Sdk.Entity)Activator.CreateInstance(result.ToType);
                            result.LogicalName = entity.LogicalName;
                            result.Constructor = type.GetConstructor(new Type[] { typeof(Microsoft.Xrm.Sdk.EntityReference) });
                        }

                        if (!isEntity && !isReference && type == typeof(Microsoft.Xrm.Sdk.EntityReference))
                        {
                            if (parameter.MatchPattern(Types.TargetAttribute, "target"))
                            {
                                isReference        = true;
                                result.IsTarget    = true;
                                result.IsReference = true;
                                result.ToType      = type;
                                result.Constructor = null;
                            }
                        }
                        #endregion

                        if (!isEntity && !isReference)
                        {
                            result.Constructor = GetConstructor(type);
                            resolvedTypes[key] = result;
                        }
                        else
                        {
                            resolvedTypes[key] = result;
                        }
                        return(result);
                    }
                }
                #endregion

                #region see if it is target, preimage post image or merged image interface
                if (type.IsInterface)
                {
                    Type toType = type.ImplementsGenericInterface(Types.Target);
                    if (toType != null)
                    {
                        var result = new TypeCache {
                            FromType = type, ToType = toType, IsTarget = true
                        };
                        var entity = (Microsoft.Xrm.Sdk.Entity)Activator.CreateInstance(result.ToType);
                        result.LogicalName = entity.LogicalName;
                        result.ResolveProperties();

                        if (ReturnIfOk(type, result))
                        {
                            resolvedTypes[key] = result;
                            return(result);
                        }
                    }

                    toType = type.ImplementsGenericInterface(Types.Preimage);
                    if (toType != null)
                    {
                        var result = new TypeCache {
                            FromType = type, ToType = toType, IsPreimage = true
                        };
                        var entity = (Microsoft.Xrm.Sdk.Entity)Activator.CreateInstance(result.ToType);
                        result.LogicalName = entity.LogicalName;
                        result.ResolveProperties();
                        if (ReturnIfOk(type, result))
                        {
                            resolvedTypes[key] = result;
                            return(resolvedTypes[key]);
                        }
                    }

                    toType = type.ImplementsGenericInterface(Types.Mergedimage);
                    if (toType != null)
                    {
                        var result = new TypeCache {
                            FromType = type, ToType = toType, IsMergedimage = true
                        };
                        var entity = (Microsoft.Xrm.Sdk.Entity)Activator.CreateInstance(result.ToType);
                        result.LogicalName = entity.LogicalName;
                        result.ResolveProperties();
                        if (ReturnIfOk(type, result))
                        {
                            resolvedTypes[key] = result;
                            return(resolvedTypes[key]);
                        }
                    }

                    toType = type.ImplementsGenericInterface(Types.Postimage);
                    if (toType != null)
                    {
                        var result = new TypeCache {
                            FromType = type, ToType = toType, IsPostimage = true
                        };
                        var entity = (Microsoft.Xrm.Sdk.Entity)Activator.CreateInstance(result.ToType);
                        result.LogicalName = entity.LogicalName;
                        result.ResolveProperties();
                        if (ReturnIfOk(type, result))
                        {
                            resolvedTypes[key] = result;
                            return(result);
                        }
                    }

                    if (type.IsITarget())
                    {
                        var entity = Extensions.Sdk.KiponSdkGeneratedExtensionMethods.ToEarlyBoundEntity(new Microsoft.Xrm.Sdk.Entity {
                            LogicalName = key.LogicalName
                        });
                        var entityType = entity.GetType();

                        if (type.IsAssignableFrom(entityType))
                        {
                            var result = new TypeCache {
                                FromType = type, ToType = entityType, IsTarget = true
                            };
                            result.LogicalName = entity.LogicalName;
                            result.ResolveProperties();

                            if (ReturnIfOk(type, result))
                            {
                                resolvedTypes[key] = result;
                                return(result);
                            }
                        }
                    }
                }
                #endregion

                #region handle shared interface by naming convention or attribute decoration
                if (type.IsInterface && !string.IsNullOrEmpty(key.LogicalName))
                {
                    var isTarget = parameter.Name == "target";

                    if (!isTarget)
                    {
                        isTarget = parameter.GetCustomAttributes(Types.TargetAttribute, false).Any();
                    }

                    if (isTarget)
                    {
                        var entity = Extensions.Sdk.KiponSdkGeneratedExtensionMethods.ToEarlyBoundEntity(new Microsoft.Xrm.Sdk.Entity {
                            LogicalName = key.LogicalName
                        });

                        var result = new TypeCache {
                            FromType = type, ToType = entity.GetType(), IsTarget = true
                        };
                        result.LogicalName = key.LogicalName;
                        result.IsGenericEntityInterface = true;
                        result.ResolveProperties();
                        result.IsImplemenedByEntity = ReturnIfImplemented(type, result);
                        resolvedTypes[key]          = result;
                        return(result);
                    }
                }

                if (type.IsInterface && !string.IsNullOrEmpty(key.LogicalName))
                {
                    var isPreimage = parameter.Name == "preimage";

                    if (!isPreimage)
                    {
                        isPreimage = parameter.GetCustomAttributes(Types.PreimageAttribute, false).Any();
                    }

                    if (isPreimage)
                    {
                        var entity = Extensions.Sdk.KiponSdkGeneratedExtensionMethods.ToEarlyBoundEntity(new Microsoft.Xrm.Sdk.Entity {
                            LogicalName = key.LogicalName
                        });

                        var result = new TypeCache {
                            FromType = type, ToType = entity.GetType(), IsPreimage = true
                        };
                        result.LogicalName = key.LogicalName;
                        result.IsGenericEntityInterface = true;
                        result.ResolveProperties();
                        result.IsImplemenedByEntity = ReturnIfImplemented(type, result);
                        resolvedTypes[key]          = result;
                        return(result);
                    }
                }

                if (type.IsInterface && !string.IsNullOrEmpty(key.LogicalName))
                {
                    var isMergedimage = parameter.Name == "mergedimage";

                    if (!isMergedimage)
                    {
                        isMergedimage = parameter.GetCustomAttributes(Types.MergedimageAttribute, false).Any();
                    }

                    if (isMergedimage)
                    {
                        var entity = Extensions.Sdk.KiponSdkGeneratedExtensionMethods.ToEarlyBoundEntity(new Microsoft.Xrm.Sdk.Entity {
                            LogicalName = key.LogicalName
                        });

                        var result = new TypeCache {
                            FromType = type, ToType = entity.GetType(), IsMergedimage = true
                        };
                        result.LogicalName = key.LogicalName;
                        result.IsGenericEntityInterface = true;
                        result.ResolveProperties();
                        result.IsImplemenedByEntity = ReturnIfImplemented(type, result);
                        resolvedTypes[key]          = result;
                        return(result);
                    }
                }

                if (type.IsInterface && !string.IsNullOrEmpty(key.LogicalName))
                {
                    var isPostimage = parameter.Name == "postimage";

                    if (!isPostimage)
                    {
                        isPostimage = parameter.GetCustomAttributes(Types.PostimageAttribute, false).Any();
                    }

                    if (isPostimage)
                    {
                        var entity = Extensions.Sdk.KiponSdkGeneratedExtensionMethods.ToEarlyBoundEntity(new Microsoft.Xrm.Sdk.Entity {
                            LogicalName = key.LogicalName
                        });

                        var result = new TypeCache {
                            FromType = type, ToType = entity.GetType(), IsPostimage = true
                        };
                        result.LogicalName = key.LogicalName;
                        result.IsGenericEntityInterface = true;
                        result.ResolveProperties();
                        result.IsImplemenedByEntity = ReturnIfImplemented(type, result);
                    }
                }
                #endregion

                #region IQueryable
                if (type.IsInterface && type.IsGenericType && type.GenericTypeArguments.Length == 1 && type.GenericTypeArguments[0].BaseType != null && type.GenericTypeArguments[0].BaseType == typeof(Microsoft.Xrm.Sdk.Entity))
                {
                    var result = ForQuery(type);
                    if (result != null)
                    {
                        result.RequireAdminService = parameter.GetCustomAttributes(Types.AdminAttribute, false).Any();
                        return(result);
                    }
                }
                #endregion

                #region IRepository
                if (type.IsInterface && type.IsGenericType && type.GenericTypeArguments.Length == 1 && type.GenericTypeArguments[0].BaseType != null && type.GenericTypeArguments[0].BaseType == typeof(Microsoft.Xrm.Sdk.Entity))
                {
                    var result = ForRepository(type);
                    if (result != null)
                    {
                        result.RequireAdminService = parameter.GetCustomAttributes(Types.AdminAttribute, false).Any();
                        return(result);
                    }
                }
                #endregion

                if (type == Types.IEntityCache)
                {
                    var result = ForEntityCache(type);
                    result.RequireAdminService = parameter.GetCustomAttributes(Types.AdminAttribute, false).Any();
                    return(result);
                }

                #region find implementing interface
                if (type.IsInterface)
                {
                    var r1 = GetInterfaceImplementation(type);

                    var    entityType        = type.ImplementsGenericInterface(Types.ActionTarget);
                    string logialName        = null;
                    bool   isActionReference = false;

                    if (entityType != null)
                    {
                        logialName        = ((Microsoft.Xrm.Sdk.Entity)Activator.CreateInstance(entityType)).LogicalName;
                        isActionReference = true;
                    }

                    var result = new TypeCache {
                        FromType = type, ToType = r1, Constructor = GetConstructor(r1), LogicalName = logialName, IsActionReference = isActionReference
                    };

                    resolvedTypes[key] = result;
                    return(result);
                }
                #endregion

                #region find relevant abstract extension
                if (type.IsAbstract)
                {
                }
                #endregion

                throw new Exceptions.UnresolvableTypeException(type);
            }
        }
Exemplo n.º 7
0
 public object[] GetCustomAttributes(bool inherit)
 {
     return(internalInfo.GetCustomAttributes(inherit));
 }