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

  • Home
  • SEARCH
  • 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 75391
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T20:30:32+00:00 2026-05-10T20:30:32+00:00

I can’t figure out why the following wont work, any ideas?? public interface IFieldSimpleItem

  • 0

I can’t figure out why the following wont work, any ideas??

public interface IFieldSimpleItem { }  public interface IFieldNormalItem : IFieldSimpleItem { }  public class Person {     public virtual T Create<T>()         where T : IFieldSimpleItem     {         return default(T);     } }  public class Bose : Person {     public override T Create<T>()         where T : IFieldNormalItem //This is where the error is     {         return default(T);     }  } 

The reason why I am doing this is due to the fact that if a developer inherits from Bose, Bose relies on the instance being creating being at least of IFieldNormalItem. Whereas the below only relies on it being IFieldSimpleItem but the above should force it to be at least IFieldNormalItem.

public class Person {     public virtual IFieldSimpleItem Create()      {         return null;     } }  public class Bose : Person {     public override IFieldSimpleItem Create()       {         return null;     }  } 

Cheers Anthony

  • 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. 2026-05-10T20:30:32+00:00Added an answer on May 10, 2026 at 8:30 pm

    I’m pretty sure you’re out of luck as far as using the compiler and generics to save you some runtime checks. You can’t override something that doesn’t already exist, and you can’t have different return types to the same methods.

    I can’t say I completely understand your motivation, but it has technical merit.

    My first attempt was using the base class having a Non-Virtual public interface, and then having another protected virtual method CheckCreatedType that would allow anything in the chain to inspect the type before the base class Create was called.

    public class A {     public IFieldSimpleItem Create()     {         IFieldSimpleItem created = InternalCreate();         CheckCreatedType(created);         return created;     }      protected virtual IFieldSimpleItem InternalCreate()     {         return new SimpleImpl();     }     protected virtual void CheckCreatedType(IFieldSimpleItem item)     {          // base class doesn't care. compiler guarantees IFieldSimpleItem     } } public class B : A {     protected override IFieldSimpleItem InternalCreate()     {         // does not call base class.         return new NormalImpl();     }     protected override void CheckCreatedType(IFieldSimpleItem item)     {         base.CheckCreatedType(item);         if (!(item is IFieldNormalItem))             throw new Exception('I need a normal item.');      } } 

    The following sticks in runtime checking at the base class. The unresolvable issue is you still have to rely on the base class method being called. A misbehaving subclass can break all checks by not calling base.CheckCreatedType(item).

    The alternatives are you hardcode all the checks for all subclasses inside the base class (bad), or otherwise externalize the checking.

    Attempt 2: (Sub)Classes register the checks they need.

    public class A {     public IFieldSimpleItem Create()     {         IFieldSimpleItem created = InternalCreate();         CheckCreatedType(created);         return created;     }      protected virtual IFieldSimpleItem InternalCreate()     {         return new SimpleImpl();     }      private void CheckCreatedType(IFieldSimpleItem item)     {         Type inspect = this.GetType();         bool keepgoing = true;         while (keepgoing)         {             string name = inspect.FullName;             if (CheckDelegateMethods.ContainsKey(name))             {                 var checkDelegate = CheckDelegateMethods[name];                 if (!checkDelegate(item))                     throw new Exception('failed check');             }             if (inspect == typeof(A))             {                 keepgoing = false;             }             else             {                 inspect = inspect.BaseType;             }         }     }      private static Dictionary<string,Func<IFieldSimpleItem,bool>> CheckDelegateMethods = new Dictionary<string,Func<IFieldSimpleItem,bool>>();     protected static void RegisterCheckOnType(string name, Func<IFieldSimpleItem,bool> checkMethod )     {         CheckDelegateMethods.Add(name, checkMethod);     } } public class B : A {     static B()     {         RegisterCheckOnType(typeof(B).FullName, o => o is IFieldNormalItem);     }      protected override IFieldSimpleItem InternalCreate()     {         // does not call base class.         return new NormalImpl();     } } 

    The check is done by the subclass registering a delegate to invoke in base class, but without the base class knowing all the rules upfront. Notice too that it’s still the Non-Virtual public interface which allows the base class to check the results before returning them.

    I’m assuming that it’s a developer error that you’re trying to catch. If it’s applicable, you can adorn the runtime check method with System.Diagnostics.Conditional('DEBUG')], allowing the Release version to skip the checks.

    My knowledge of generics isn’t perfect, so maybe this is unnecessary. However the checks here don’t have to be for type alone: this could be adapted for other uses. e.g. the delegate passed in Register.. doesn’t have to just check the reference is a specific type’

    * Note that it’s probably not good to create the dictionary on the type name as written above; this working is a little simplistic in order to illustrate the mechanism used.

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

Sidebar

Related Questions

I need to solve the following question which i can't get to work by
Can anyone (maybe an XSL-fan?) help me find any advantages with handling presentation of
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Can I use the following across all browsers? <a href=# onclick=doSomething()>Click here.</a> Is this
Can anyone tell me what's wrong with this robots.txt? http://bizup.cloudapp.net/robots.txt The following is the
Can i get the source code for a WAMP stack installer somewhere? Any help
Can somebody point me to a resource that explains how to go about having
Can you cast a List<int> to List<string> somehow? I know I could loop through
can you recommend some good ASP.NET tutorials or a good book? Should I jump
Can a LINQ enabled app run on a machine that only has the .NET

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.