当我尝试按名称获取属性时,我在NHibernate中遇到“模糊匹配”异常。我正在尝试远程调试它,因为它不会发生在我的机器上,只发生在Web服务器上:/所以我随时吐出所有可见的属性,我明白了:
Failed to get property Id on UserProxy49b5a83368564e9cbd22b8e2f0a0c5a7!
All properties:
property: FirstName of type: System.String readable: True writable: True declared type: UserProxy49b5a83368564e9cbd22b8e2f0a0c5a7
... etc for other properties
property: Id of type: System.Nullable`1[System.Int32] readable: True writable: True declared type: UserProxy49b5a83368564e9cbd22b8e2f0a0c5a7
... etc for other properties
property: HibernateLazyInitializer of type: NHibernate.Proxy.ILazyInitializer readable: True writable: False declared type: UserProxy49b5a83368564e9cbd22b8e2f0a0c5a7
property: Id of type: System.Nullable`1[System.Int32] readable: True writable: True declared type: MyNamespace.ModelBase`1[System.Nullable`1[System.Int32]]
ModelBase是一个定义“Id”的抽象基类,因此存在歧义。但是有两个问题让我感到困惑:怎么样?和为什么不在我的机器上?我也与这些代理对象进行交互,从来没有出现歧义,但每次都会发生这种情况在服务器上。
这样的歧义怎么会存在呢?我已经尝试将绑定标志更改为.Instance | .Public
,但我仍然得到错误(我每次都会更改我的错误输出,因此它不仅仅是一个陈旧的二进制文件)。所以该对象有两个相同类型和相同名称的属性,.NET允许这个......?
要添加更多问题,我该如何解决此问题,是否有人有任何远程调试的建议?我现在依赖异常并手动向输出添加信息,这几乎没有效率。
编辑:投掷线周围的确切代码:
PropertyInfo prop = null;
try {
prop = root.GetType().GetProperty(props[0], BindingFlags.Instance | BindingFlags.Public); // props[0] == "Id"
}
catch (Exception e) {
PropertyInfo[] allprops = null;
allprops = root.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);
StringBuilder sb = new StringBuilder();
foreach (PropertyInfo p in allprops)
sb.Append("property: " + p.Name + " of type: " + p.PropertyType + " readable: " + p.CanRead + " writable: " + p.CanWrite + " declared type: " + p.DeclaringType);
throw new Exception("Failed to get public property " + props[0] + " on " + root.GetType().FullName + "! All: " + sb.ToString() + " Inner: " + e.Message + " inner stack: " + e.StackTrace, e);
}
答案 0 :(得分:0)
由于您未指定IgnoreCase,我假设您的模板匹配是由代理继承引起的。
也许您的本地实例具有不同的延迟设置,这会阻止将实体作为代理对象加载?
(UserProxy49b5a83368564e9cbd22b8e2f0a0c5a7表明它是代理对象)
您确定要正确覆盖会员吗?你试过BindingFlags.DeclaredOnly吗?我想知道它是否也试图返回继承的成员,这就是为什么你会收到一个模糊的匹配例外。