【发布时间】:2016-02-04 06:11:04
【问题描述】:
我在使用扩展方法时遇到问题。另一种方法导致了同样的错误,现在我又遇到了同样的问题。编译时 - Visual Studio 将我的扩展方法标记为模棱两可。但我很确定这是我唯一的实现 -
我的扩展方法是——
public static class TypeInfoExtensions
{
/// <summary>
/// Gets the property dictionary.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="types">The types.</param>
/// <returns></returns>
public static IEnumerable<Dictionary<string, object>> GetPropertyDictionaryList<T>(this IEnumerable<TypeInfo> types) where T : Attribute
{
return types.Select(x => x.GetPropertyDictionary<T>());
}
/// <summary>
/// Gets the property dictionary.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="type">The type.</param>
/// <returns></returns>
public static Dictionary<string, object> GetPropertyDictionary<T>(this TypeInfo type) where T : Attribute
{
var dict = new Dictionary<string, object>();
return dict;
}
}
而且是这样使用的——
var model = ...... // other codes
var parameters = Assembly.GetAssembly(typeof (Model.Domain.Tenants.Tenant)).DefinedTypes;
model.AvailableProperties = parameters.GetPropertyDictionaryList<MessageTemplateItem>().ToList()
这是我唯一的实现,我刚刚添加了它,但是 VS 显示这个错误 -
错误 81 以下方法之间的调用不明确或 特性: 'SMP.Core.Extensions.TypeInfoExtensions.GetPropertyDictionary(System.Reflection.TypeInfo)' 和 'SMP.Core.Extensions.TypeInfoExtensions.GetPropertyDictionary(System.Reflection.TypeInfo)'
我认为这可能是一个临时错误,可能是清理解决方案而不是清理引用或缓存。但如果有其他人遇到此错误,我将非常感谢您的反馈。
更新奇怪的是,这段代码有效 -
public static IEnumerable<Dictionary<string, object>> GetPropertyDictionaryList<T>(this IEnumerable<TypeInfo> types) where T : Attribute
{
return types.Select(x => GetPropertyDictionary<T>(x));
}
我现在真的很困惑,到目前为止我知道这些对于扩展方法是相同的 -
GetPropertyDictionary<T>(x)
x.GetPropertyDictionary<T>()
各位有什么想法吗?
【问题讨论】:
-
重启VS看看是否消失
-
我在使用动态类型的扩展方法时遇到了问题。不要使用“var parameters =”,而是使用实际类型,看看是否可以为您解决问题。
-
我编译了你的代码,它工作正常。尝试从 bin 和 obj 文件夹中删除所有 dll,看看是否有效。
-
@ELewis 我认为这不是问题。它调用成功,这是该方法内的第二次调用,导致问题。
-
你能提供一个minimal reproducible example吗?这样可以更轻松地为您提供帮助。