代码之家  ›  专栏  ›  技术社区  ›  Richard Friend

尝试使用xsd.exe生成xsd:schema时出现错误

  •  3
  • Richard Friend  · 技术社区  · 14 年前

    我有一个自定义配置部分,我想使用xsd工具生成一个xsd方案,但是我得到以下错误:

    Microsoft (R) Xml Schemas/DataTypes support utility
    [Microsoft (R) .NET Framework, Version 4.0.30319.1]
    Copyright (C) Microsoft Corporation. All rights reserved.
    Error: There was an error processing 'C:\Development\XXX Application Framewo
    rk\XXX.AppFramework\bin\Debug\XXX.AppFramework.dll'.
    - There was an error reflecting type 'XXX.AppFramework.Configuration.AppFr
    ameworkEnvironmentConfiguration'.
    - You must implement a default accessor on System.Configuration.ConfigurationL
    ockCollection because it inherits from ICollection.
    

    public class AppFrameworkEnvironmentConfiguration : ConfigurationElement
    {
        [ConfigurationProperty("Name")]
        public string Name
        {
            get { return (string)this["Name"]; }
            set { this["Name"] = value; }
        }
    
        [ConfigurationProperty("Description")]
        public string Description
        {
            get { return (string)this["Description"]; }
            set { this["Description"] = value; }
        }
    
        [ConfigurationProperty("ApplicationName")]
        public string ApplicationName
        {
            get { return (string)this["ApplicationName"]; }
            set { this["ApplicationName"] = value; }
        }
    
        [ConfigurationProperty("DeploymentMode",DefaultValue=DeploymentMode.Local)]
        public DeploymentMode DeploymentMode
        {
            get { return (DeploymentMode)this["DeploymentMode"]; }
            set { this["DeploymentMode"] = value; }
        }
    
    
    }
    

    它看起来不像ConfigurationElement实现ICollection,所以我不确定为什么会出现此错误?

    通过反射器深入观察发现这个错误是何时抛出的,我更加困惑,首先,我的小测试确实评估为错误:

    bool test = typeof(ICollection).IsAssignableFrom(typeof    (XXX.AppFramework.Configuration.AppFrameworkEnvironmentConfiguration));
    

    在reflector中,我唯一能找到它引发异常的地方是在这个精确的测试中包装的,我在System.Xml.Reflection.TypeScope.ImportTypeDesc中找到它,如下所示:

    else if (typeof(ICollection).IsAssignableFrom(type))
    {
        root = TypeKind.Collection;
        elementType = GetCollectionElementType(type, (memberInfo == null) ? null : (memberInfo.DeclaringType.FullName + "." + memberInfo.Name));
        none |= GetConstructorFlags(type, ref exception);
    }
    

    然后

    private static Type GetCollectionElementType(Type type, string memberInfo)
    {
    return GetDefaultIndexer(type, memberInfo).PropertyType;
    }
    

    internal static PropertyInfo GetDefaultIndexer(Type type, string memberInfo)
    {
    if (typeof(IDictionary).IsAssignableFrom(type))
    {
        if (memberInfo == null)
        {
            throw new NotSupportedException(Res.GetString("XmlUnsupportedIDictionary",     new     object[] { type.FullName }));
        }
        throw new NotSupportedException(Res.GetString("XmlUnsupportedIDictionaryDetails", new object[] { memberInfo, type.FullName }));
    }
    MemberInfo[] defaultMembers = type.GetDefaultMembers();
    PropertyInfo info = null;
    if ((defaultMembers != null) && (defaultMembers.Length > 0))
    {
        for (Type type2 = type; type2 != null; type2 = type2.BaseType)
        {
            for (int i = 0; i < defaultMembers.Length; i++)
            {
                if (defaultMembers[i] is PropertyInfo)
                {
                    PropertyInfo info2 = (PropertyInfo) defaultMembers[i];
                    if ((info2.DeclaringType == type2) && info2.CanRead)
                    {
                        ParameterInfo[] parameters = info2.GetGetMethod().GetParameters();
                        if ((parameters.Length == 1) && (parameters[0].ParameterType == typeof(int)))
                        {
                            info = info2;
                            break;
                        }
                    }
                }
            }
            if (info != null)
            {
                break;
            }
        }
    }
    if (info == null)
    {
        throw new InvalidOperationException(Res.GetString("XmlNoDefaultAccessors", new object[] { type.FullName })); **< HERE IS THE ERROR**
    }
    if (type.GetMethod("Add", new Type[] { info.PropertyType }) == null)
    {
        throw new InvalidOperationException(Res.GetString("XmlNoAddMethod", new object[] { type.FullName, info.PropertyType, "ICollection" }));
    }
    return info;
    }
    

    我还尝试添加了一个默认的无参数构造函数,没有不同-( 有什么想法吗?

    1 回复  |  直到 14 年前
        1
  •  0
  •   Mr. Mr.    14 年前

    你能试着添加一个默认构造函数吗,我已经看到类似的问题了,添加一个空的默认构造函数已经解决了这个问题。不知道为什么,但一直有研究的意义,在某一点上。