System.Xml.Serialization.TypeScope.GetDefaultIndexer C# (CSharp) Method

GetDefaultIndexer() static private method

static private GetDefaultIndexer ( Type type, string memberInfo ) : PropertyInfo
type System.Type
memberInfo string
return System.Reflection.PropertyInfo
        internal static PropertyInfo GetDefaultIndexer(Type type, string memberInfo)
        {
            if (typeof(IDictionary).IsAssignableFrom(type))
            {
                if (memberInfo == null)
                {
                    throw new NotSupportedException(SR.Format(SR.XmlUnsupportedIDictionary, type.FullName));
                }
                else
                {
                    throw new NotSupportedException(SR.Format(SR.XmlUnsupportedIDictionaryDetails, memberInfo, type.FullName));
                }
            }

            MemberInfo[] defaultMembers = type.GetDefaultMembers();
            PropertyInfo indexer = null;
            if (defaultMembers != null && defaultMembers.Length > 0)
            {
                for (Type t = type; t != null; t = t.GetTypeInfo().BaseType)
                {
                    for (int i = 0; i < defaultMembers.Length; i++)
                    {
                        if (defaultMembers[i] is PropertyInfo)
                        {
                            PropertyInfo defaultProp = (PropertyInfo)defaultMembers[i];
                            if (defaultProp.DeclaringType != t) continue;
                            if (!defaultProp.CanRead) continue;
                            MethodInfo getMethod = defaultProp.GetMethod;
                            ParameterInfo[] parameters = getMethod.GetParameters();
                            if (parameters.Length == 1 && parameters[0].ParameterType == typeof(int))
                            {
                                indexer = defaultProp;
                                break;
                            }
                        }
                    }
                    if (indexer != null) break;
                }
            }
            if (indexer == null)
            {
                throw new InvalidOperationException(SR.Format(SR.XmlNoDefaultAccessors, type.FullName));
            }
            MethodInfo addMethod = type.GetMethod("Add", new Type[] { indexer.PropertyType });
            if (addMethod == null)
            {
                throw new InvalidOperationException(SR.Format(SR.XmlNoAddMethod, type.FullName, indexer.PropertyType, "ICollection"));
            }
            return indexer;
        }
        private static Type GetCollectionElementType(Type type, string memberInfo)

Usage Example

Example #1
0
        private string WriteDefaultIndexerInit(Type type, string escapedName, bool collectionUseReflection, bool elementUseReflection)
        {
            string       s = this.GenerateVariableName("item", escapedName);
            PropertyInfo defaultIndexer = TypeScope.GetDefaultIndexer(type, null);

            this.writer.Write("static XSArrayInfo ");
            this.writer.Write(s);
            this.writer.Write("= new XSArrayInfo(");
            this.writer.Write(this.GetStringForTypeof(CodeIdentifier.GetCSharpName(type), collectionUseReflection));
            this.writer.Write(".GetProperty(");
            this.WriteQuotedCSharpString(defaultIndexer.Name);
            this.writer.Write(",");
            this.writer.Write(this.GetStringForTypeof(CodeIdentifier.GetCSharpName(defaultIndexer.PropertyType), elementUseReflection));
            this.writer.Write(",new ");
            this.writer.Write(typeof(Type[]).FullName);
            this.writer.WriteLine("{typeof(int)}));");
            this.reflectionVariables.Add("0:" + escapedName, s);
            return(s);
        }