0

我正在尝试一种通用存储库方法,在控制器类中

var result = typeof(ISiteRepository)
      .GetMethod("UpdateLanguage")
      .MakeGenericMethod(objectType)
      .Invoke(this.siteRepository, new object[] { obj });

它在下面给了我一个错误,例如

GenericArguments[0], 'Site.City', , on 'Void UpdateLanguageT' 违反了类型 'T' 的约束。

领域类

public abstract class Entity : IEntity
 {
  [Key]
  public virtual int Id { get; set; }

 }

 public class Language:Entity
 {
  public string Name { get; set; }
  public int LanguageId { get; set; }
 }

 public abstract class Language<T> : Language where T : Language
 {
  public ICollection<T> Languages { get; set; }
 }

 public class City : Language<CityLanguage>
 {
  public int CountryId { get; set; }
  public string Code { get; set; }
  public virtual Country Country { get; set; }
 }

 public class CityLanguage : Language
 {
  public string Description { get; set; }
 }

存储库类


 public interface ISiteRepository
 {
  void UpdateLanguage<T>(T entity) where T : Language<T>;
 }

 public class SiteRepository : ISiteRepository
 {
  public SiteRepository(DbContext context)
   : base(context)
  {
  }

 public void UpdateLanguage<T>(T entity) where T : Language<T>
  {
      var item = GetByKey<T>(entity.Id);
      var languages = entity.Languages;

   }


 }

控制器


 public partial class SiteSettingsController : BaseController
 {

ISiteRepository siteRepository;

public SiteSettingsController(ISiteRepository siteRepository)
{
 this.siteRepository = siteRepository;
}

[HttpPost]
public virtual ActionResult LanguageObjectUpdate(LanguageListModel model,string type)
{

  //type like City
 Type objectType = Type.GetType(type);

 var obj = Activator.CreateInstance(objectType);

 obj.GetType().GetProperty("Id").SetValue(obj, model.Id, null);

 Type languageObjectType = objectType.GetProperty("Languages").DeclaringType.GetGenericArguments()[0];

 object objectLanguages = GetGenericListInstance(languageObjectType);

 foreach (var language in model.Languages)
 {
  object languageObject = Activator.CreateInstance(languageObjectType);

  languageObject.GetType().GetProperty("LanguageId").SetValue(languageObject, language.LanguageId, null);

  foreach (var languageItem in language.Items)
  {
   languageObject.GetType().GetProperty(languageItem.Label).SetValue(languageObject, languageItem.Value, null);
  }


  ((IList)objectLanguages).Add(languageObject);
 }

 obj.GetType().GetProperty("Languages").SetValue(obj, objectLanguages, null);

 var result = typeof(ISiteRepository)
  .GetMethod("UpdateLanguage")
  .MakeGenericMethod(objectType)
  .Invoke(this.siteRepository, new object[] { obj });

 return PartialView("Result";
}

private static object GetGenericListInstance(Type languageObjectType)
{
 Type listType = typeof(List<>);

 Type[] listTypeGenericArguments = { languageObjectType };

 Type objectLanguagesType = listType.MakeGenericType(listTypeGenericArguments);

 object objectLanguages = Activator.CreateInstance(objectLanguagesType);
 return objectLanguages;
}
4

1 回答 1

2

您的通用方法UpdateLanguage<T>(T entity)必须TLanguage<T>. 但既然Language<>是一门课,那似乎有点难获得。您得到的错误表明不满足约束。

于 2012-05-08T08:30:10.520 回答