Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7918189
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T15:25:19+00:00 2026-06-03T15:25:19+00:00

i am trying a kind of generic repository method,in controller class var result =

  • 0

i am trying a kind of generic repository method,in controller class

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

it given an error me below like

GenericArguments[0], ‘Site.City’, , on ‘Void UpdateLanguageT’ violates the constraint of type ‘T’.

Domain classes

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; }
 }

Repository class


 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;

   }


 }

Controller


 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;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T15:25:20+00:00Added an answer on June 3, 2026 at 3:25 pm

    Your generic method UpdateLanguage<T>(T entity) requires T to be a Language<T>. But since Language<> is a class, that seems a little hard to obtain. The error you get tells that the constraint is not met.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to create a generic build template for my Makefiles, kind of like
I'm trying to create a generic method that will return a predicate to find
I am trying to read generic info from a class. Here is what I
I am trying to create a generic Get method for Entity Framework, with a
I am trying to do kind of a wizard using thickbox, and it works
i'm trying to implement kind of speedometer. i'm getting informations about rounds per minute,
I am trying to make a special kind of jtable. I want the entire
I'm trying to have a kind of dirty underline effect using a string of
What I am trying to do is kind of like this: I have datatypes
I'm trying to make some kind of top for some game stadistics Table it's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.