我有一个自动映射器的接口。DTO 实现了这个接口。如您所见,有一个默认方法。
public interface IMap<T> {
public void Mapping(Profile profile) {
profile.CreateMap(typeof(T), GetType()).ReverseMap();
}
}
public class ItemDto : IMap<Item> {
public string Name { get; set; }
}
当我尝试调用此方法时。找不到方法。
public class MappingProfile : Profile {
public MappingProfile() {
ApplyMappingsFromAssembly();
}
private void ApplyMappingsFromAssembly() {
var types = AppDomain.CurrentDomain.GetAssemblies().Where(w => !w.IsDynamic).SelectMany(s => s.GetExportedTypes())
.Where(t => t.GetInterfaces().Any(i =>
i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IMap<>)))
.ToList();
foreach (var type in types) {
var instance = Activator.CreateInstance(type);
var methodInfo = type.GetMethod("Mapping");
//In here I expect to call default interface method.
methodInfo?.Invoke(instance, new object[] { this });
}
}
}
如何调用默认接口方法?