static private PropertyNotDefinedForType ( object p0, object p1, string paramName ) : Exception | ||
p0 | object | |
p1 | object | |
paramName | string | |
Результат | Exception |
public static ConditionalMemberCSharpExpression ConditionalProperty(Expression expression, PropertyInfo property) { RequiresCanRead(expression, nameof(expression)); ContractUtils.RequiresNotNull(property, nameof(property)); if (!property.CanRead) { throw Error.ConditionalAccessRequiresReadableProperty(); } if (property.GetIndexParameters().Length != 0) { throw Error.ConditionalAccessRequiresReadableProperty(); } if (property.GetGetMethod(true).IsStatic) { throw Error.ConditionalAccessRequiresNonStaticMember(); } var type = expression.Type.GetNonNullReceiverType(); if (!TypeUtils.IsValidInstanceType(property, type)) { throw LinqError.PropertyNotDefinedForType(property, type); } return(ConditionalMemberCSharpExpression.Make(expression, property)); }
public static MemberExpression Property(Expression expression, PropertyInfo property) { ContractUtils.RequiresNotNull(property, "property"); MethodInfo mi = property.GetGetMethod(true) ?? property.GetSetMethod(true); if (mi == null) { throw Error.PropertyDoesNotHaveAccessor(property); } if (mi.IsStatic) { ContractUtils.Requires(expression == null, "expression", Strings.OnlyStaticPropertiesHaveNullInstance); } else { ContractUtils.Requires(expression != null, "property", Strings.OnlyStaticPropertiesHaveNullInstance); RequiresCanRead(expression, "expression"); if (!TypeUtils.IsValidInstanceType(property, expression.Type)) { throw Error.PropertyNotDefinedForType(property, expression.Type); } } return(MemberExpression.Make(expression, property)); }
/// <summary> /// Creates a <see cref="MemberExpression"/> accessing a property. /// </summary> /// <param name="expression">The containing object of the property. This can be null for static properties.</param> /// <param name="type">The <see cref="Type"/> containing the property.</param> /// <param name="propertyName">The property to be accessed.</param> /// <returns>The created <see cref="MemberExpression"/>.</returns> public static MemberExpression Property(Expression expression, Type type, string propertyName) { ContractUtils.RequiresNotNull(type, nameof(type)); ContractUtils.RequiresNotNull(propertyName, nameof(propertyName)); // bind to public names first PropertyInfo pi = type.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.FlattenHierarchy) ?? type.GetProperty(propertyName, BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase | BindingFlags.FlattenHierarchy); if (pi == null) { throw Error.PropertyNotDefinedForType(propertyName, type, nameof(propertyName)); } return(Property(expression, pi)); }
public static MemberExpression Property(Expression expression, PropertyInfo property) { ContractUtils.RequiresNotNull(property, nameof(property)); var mi = property.GetGetMethod(nonPublic: true); if (mi == null) { mi = property.GetSetMethod(nonPublic: true); if (mi == null) { throw Error.PropertyDoesNotHaveAccessor(property, nameof(property)); } if (mi.GetParameters().Length != 1) { throw Error.IncorrectNumberOfMethodCallArguments(mi, nameof(property)); } } else if (mi.GetParameters().Length != 0) { throw Error.IncorrectNumberOfMethodCallArguments(mi, nameof(property)); } if (mi.IsStatic) { if (expression != null) { throw Error.OnlyStaticPropertiesHaveNullInstance(nameof(expression)); } } else { if (expression == null) { throw Error.OnlyStaticPropertiesHaveNullInstance(nameof(property)); } ExpressionUtils.RequiresCanRead(expression, nameof(expression)); if (!TypeUtils.IsValidInstanceType(property, expression.Type)) { throw Error.PropertyNotDefinedForType(property, expression.Type, nameof(property)); } } ValidateMethodInfo(mi, nameof(property)); return(MemberExpression.Make(expression, property)); }
//CONFORMING public static MemberExpression Property(Expression expression, string propertyName) { RequiresCanRead(expression, "expression"); // bind to public names first PropertyInfo pi = expression.Type.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.FlattenHierarchy); if (pi == null) { pi = expression.Type.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase | BindingFlags.FlattenHierarchy); } if (pi == null) { throw Error.PropertyNotDefinedForType(propertyName, expression.Type); } return(Property(expression, pi)); }
public static MemberExpression Property(Expression expression, PropertyInfo property) { ContractUtils.RequiresNotNull(property, "property"); MethodInfo mi = property.GetGetMethod(true); if (mi == null) { mi = property.GetSetMethod(true); if (mi == null) { throw Error.PropertyDoesNotHaveAccessor(property); } else if (mi.GetParametersCached().Length != 1) { throw Error.IncorrectNumberOfMethodCallArguments(mi); } } else if (mi.GetParametersCached().Length != 0) { throw Error.IncorrectNumberOfMethodCallArguments(mi); } if (mi.IsStatic) { if (expression != null) { throw new ArgumentException(Strings.OnlyStaticPropertiesHaveNullInstance, "expression"); } } else { if (expression == null) { throw new ArgumentException(Strings.OnlyStaticPropertiesHaveNullInstance, "property"); } RequiresCanRead(expression, "expression"); if (!TypeUtils.IsValidInstanceType(property, expression.Type)) { throw Error.PropertyNotDefinedForType(property, expression.Type); } } return(MemberExpression.Make(expression, property)); }
/// <summary> /// Creates a <see cref="MemberExpression"/> accessing a property. /// </summary> /// <param name="expression">The containing object of the property. This can be null for static properties.</param> /// <param name="type">The <see cref="Type"/> containing the property.</param> /// <param name="propertyName">The property to be accessed.</param> /// <returns>The created <see cref="MemberExpression"/>.</returns> public static MemberExpression Property( Expression?expression, [DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)] Type type, string propertyName) { ArgumentNullException.ThrowIfNull(type); ArgumentNullException.ThrowIfNull(propertyName); // bind to public names first PropertyInfo?pi = type.GetProperty(propertyName, BindingFlags.Instance | BindingFlags.Static | BindingFlags.Public | BindingFlags.IgnoreCase | BindingFlags.FlattenHierarchy) ?? type.GetProperty(propertyName, BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.IgnoreCase | BindingFlags.FlattenHierarchy); if (pi == null) { throw Error.PropertyNotDefinedForType(propertyName, type, nameof(propertyName)); } return(Property(expression, pi)); }
/// <summary> /// Creates an expression representing an indexer access operation. /// </summary> /// <param name="object">The object to access.</param> /// <param name="argument">The argument that will be used to index or slice the object.</param> /// <param name="lengthOrCount">The property used to retrieve the element count of the object getting accessed.</param> /// <param name="indexOrSlice">The member used to index or slice the object.</param> /// <returns>A new <see cref="IndexerAccessCSharpExpression"/> instance representing the array access operation.</returns> public static IndexerAccessCSharpExpression IndexerAccess(Expression @object, Expression argument, PropertyInfo lengthOrCount, MemberInfo indexOrSlice) { RequiresCanRead(@object, nameof(@object)); // // The argument can be of type Index or Range. We'll check indexOrSlice accordingly below. // RequiresCanRead(argument, nameof(argument)); if (argument.Type != typeof(Index) && argument.Type != typeof(Range)) { throw Error.InvalidIndexerAccessArgumentType(argument.Type); } // // A type is Countable if it has a property named Length or Count with an accessible getter and a return type of int. // lengthOrCount ??= FindCountProperty("Length") ?? FindCountProperty("Count"); PropertyInfo FindCountProperty(string name) => @object.Type.GetProperty(name, BindingFlags.Public | BindingFlags.Instance, binder: null, typeof(int), Type.EmptyTypes, modifiers: null); ContractUtils.RequiresNotNull(lengthOrCount, nameof(lengthOrCount)); var lengthOrCountGetMethod = lengthOrCount.GetGetMethod(nonPublic: true); // NB: System.Linq.Expressions allows non-public properties. if (lengthOrCountGetMethod == null) { throw LinqError.PropertyDoesNotHaveAccessor(lengthOrCount); } if (lengthOrCountGetMethod.IsStatic) { throw Error.AccessorCannotBeStatic(lengthOrCountGetMethod); } if (lengthOrCountGetMethod.GetParametersCached().Length != 0) { throw LinqError.IncorrectNumberOfMethodCallArguments(lengthOrCountGetMethod); } if (!TypeUtils.IsValidInstanceType(lengthOrCount, @object.Type)) { throw LinqError.PropertyNotDefinedForType(lengthOrCount, @object.Type); } if (lengthOrCount.PropertyType != typeof(int)) { throw Error.InvalidLengthOrCountPropertyType(lengthOrCount); } ValidateMethodInfo(lengthOrCountGetMethod); if (argument.Type == typeof(Index)) { // // The type has an accessible instance indexer which takes a single int as the argument. // indexOrSlice ??= FindIndexer(); PropertyInfo FindIndexer() { var indexers = (from p in @object.Type.GetProperties(BindingFlags.Public | BindingFlags.Instance) let i = p.GetIndexParameters() where i.Length == 1 && i[0].ParameterType == typeof(int) select p) .ToArray(); return(indexers.Length == 1 ? indexers[0] : null); } ContractUtils.RequiresNotNull(indexOrSlice, nameof(indexOrSlice)); var index = indexOrSlice as PropertyInfo ?? GetProperty(indexOrSlice as MethodInfo ?? throw Error.InvalidIndexMember(indexOrSlice)); indexOrSlice = index; // NB: Store the property rather than a method. var indexAccessor = index.GetGetMethod(nonPublic: true); // NB: System.Linq.Expressions allows non-public properties. if (indexAccessor == null) { indexAccessor = index.GetSetMethod(nonPublic: true) ?? throw LinqError.PropertyDoesNotHaveAccessor(indexOrSlice); if (indexAccessor.GetParametersCached().Length != 2) { throw LinqError.IncorrectNumberOfMethodCallArguments(indexAccessor); } } else if (indexAccessor.GetParametersCached().Length != 1) { throw LinqError.IncorrectNumberOfMethodCallArguments(indexAccessor); } if (indexAccessor.IsStatic) { throw Error.AccessorCannotBeStatic(indexAccessor); } if (!TypeUtils.IsValidInstanceType(indexAccessor, @object.Type)) { throw LinqError.PropertyNotDefinedForType(indexAccessor, @object.Type); } if (indexAccessor.GetParametersCached()[0].ParameterType != typeof(int)) { throw Error.InvalidIndexerParameterType(indexOrSlice); } ValidateMethodInfo(indexAccessor); } else { // // The type has an accessible member named Slice which has two parameters of type int. // Debug.Assert(argument.Type == typeof(Range)); indexOrSlice ??= FindSliceMethod(); MethodInfo FindSliceMethod() => @object.Type.GetMethod(@object.Type == typeof(string) ? "Substring" : "Slice", BindingFlags.Public | BindingFlags.Instance, binder: null, new[] { typeof(int), typeof(int) }, modifiers: null); ContractUtils.RequiresNotNull(indexOrSlice, nameof(indexOrSlice)); var slice = indexOrSlice as MethodInfo ?? throw Error.InvalidSliceMember(indexOrSlice); ValidateMethodInfo(slice); if (slice.IsStatic) { throw Error.SliceMethodMustNotBeStatic(slice); } ValidateCallInstanceType(@object.Type, slice); var sliceParams = slice.GetParametersCached(); if (sliceParams.Length != 2 || sliceParams[0].ParameterType != typeof(int) || sliceParams[1].ParameterType != typeof(int)) { throw Error.InvalidSliceParameters(slice); } } return(new IndexerAccessCSharpExpression(@object, argument, lengthOrCount, indexOrSlice)); }