离开@thehenny的注释,我得到了一些不仅有效,而且可以处理应用于属性或方法的[TopicKey]属性的东西。为了满足我的需要,我只希望它在一个界面中出现一次,但是其他人可以根据自己的需要扩展这个解决方案
subscriberTypeToTopicKeySelector = iType =>
{
var instance = kernel.Get(iType);
var classType = instance.GetType();
var interfaceMap = classType.GetInterfaceMap(iType);
var iTopicKeyPropertyGetMethods = iType.GetProperties()
.Where(x => x.HasAttribute<TopicKeyAttribute>())
.Select(x => x.GetMethod);
var iTopicKeyMethods = iType.GetMethods()
.Where(x => x.HasAttribute<TopicKeyAttribute>())
.Union(iTopicKeyPropertyGetMethods);
var indexOfInterfaceMethod = Array.IndexOf(interfaceMap.InterfaceMethods, iTopicKeyMethods.Single());
var classMethodInfo = interfaceMap.TargetMethods[indexOfInterfaceMethod];
return classMethodInfo.Invoke(instance, BindingFlags.Default, null, null, CultureInfo.CurrentCulture)
.ToString();
};