I’m getting an “Ambiguous match found” exception in NHibernate when I try to get a property by name. I’m attempting to debug it remotely, as it doesn’t happen on my machine, only on the web server :/ So I spat out all the properties visible at any time, and I get this:
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 is an abstract base class which defines “Id”, so there’s the ambiguity. But two questions are stumping me: how? and why not on my machine? I interact with those proxy objects too, there has never been an ambiguity, but it happens every time on the server.
How can an ambiguity like that even exist? I’ve tried changing my binding flags to .Instance | .Public, but I still get the error (and I change my error output a bit each time, so it’s not just a stale binary). So the object has two properties of identical types and identical names, and .NET allows this…?
To add a few more questions, how can I resolve this, and does anyone have any suggestions for remote debugging? I’m relying on exceptions and manually adding information to the output right now, and that’s hardly efficient.
edit: exact code around the throwing line:
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);
}
Since you don’t specify IgnoreCase I assume that your ambigous match is caused by the proxy inheritance.
Maybe your local instance has different lazy settings which prevents the entity to be loaded as a proxy object?
(UserProxy49b5a83368564e9cbd22b8e2f0a0c5a7 indicates that it is a proxy object)
Are you sure that you are overriding the member correctly? Have you tried BindingFlags.DeclaredOnly? I’m wondering if it tries to return inherited members as well, and that is why you receive the ambigous match exception.