我試圖做一個函數,將eUsers列表對象映射到用戶列表對象。這實際上將映射幾個屬性,但是當propertyInfo.Name == "UserName"
,它會扔我一個AmbiguousMatchException
以下行:C#AmbiguousMatchException當GetProperty並試圖映射兩個對象與海誓山盟
typeof(User)
.GetProperty(ePropertyInfo.Name, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetField)
.SetValue(user, ePropertyInfo.GetValue(currentEUser));
我測試,似乎它正在由GetProperty方法觸發。我不確定可能會導致這種情況。以下是完整的功能:
private List<User> ConvertEUsersToUsers(List<eUser> eUsers)
{
List<User> users = new List<User>();
User user = null;
IList<PropertyInfo> eUserProps = new List<PropertyInfo>(eUsers.FirstOrDefault().GetType().GetProperties());
IList<PropertyInfo> userProps = typeof(User).GetProperties();
foreach (eUser currentEUser in eUsers)
{
user = new User();
foreach (PropertyInfo ePropertyInfo in eUserProps)
{
foreach (PropertyInfo propertyInfo in userProps)
{
if (propertyInfo.Name == ePropertyInfo.Name)
{
if (ePropertyInfo.CanWrite)
{
typeof(User)
.GetProperty(ePropertyInfo.Name, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public | BindingFlags.SetField)
.SetValue(user, ePropertyInfo.GetValue(currentEUser));
}
break;
}
}
}
users.Add(user);
}
return users;
}
編輯:
下面是用戶和EUSER類的一部分:
[Serializable]
[Application(7)]
[Table("Users")]
public partial class User : MembershipUser, IPrincipal, IIdentity, IEntity, IIdNameClass {
[Column("Name")]
public new EntityField UserName { get; set; }
}
[Table("Users")]
public class eUser : User
{
[NotMapped]
public Boolean selected { get; set; }
public new UserTypes UserType { get; set; }
public eUser() : base()
{
selected = false;
}
}
你可以顯示'User'類嗎? – stuartd
你試過刪除這個BindingFlags.IgnoreCase嗎? –
@stuartd添加了類 –