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 702387
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T03:42:54+00:00 2026-05-14T03:42:54+00:00

UPDATE: I’ve changed the wording of the question. Previously it was a yes/no question

  • 0

UPDATE: I’ve changed the wording of the question. Previously it was a yes/no question about if a base class could be changed at runtime.

I may be working on mission impossible here, but I seem to be getting close. I want to extend a ASP.NET control, and I want my code to be unit testable. Also, I’d like to be able to fake behaviors of a real Label (namely things like ID generation, etc), which a real Label can’t do in an nUnit host.

Here a working example that makes assertions on something that depends on a real base class and something that doesn’t– in a more realistic unit test, the test would depend on both –i.e. an ID existing and some custom behavior.

Anyhow the code says it better than I can:

public class LabelWrapper : Label //Runtime
//public class LabelWrapper : FakeLabel //Unit Test time
{
    private readonly LabelLogic logic= new LabelLogic();

    public override string Text
    {
        get
        {
            return logic.ProcessGetText(base.Text);
        }
        set
        {
            base.Text=logic.ProcessSetText(value);
        }
    }
}

//Ugh, now I have to test FakeLabelWrapper
public class FakeLabelWrapper : FakeLabel //Unit Test time
{
    private readonly LabelLogic logic= new LabelLogic();

    public override string Text
    {
        get
        {
            return logic.ProcessGetText(base.Text);
        }
        set
        {
            base.Text=logic.ProcessSetText(value);
        }
    }
}

[TestFixture]
public class UnitTest
{
    [Test]
    public void Test()
    {
        //Wish this was LabelWrapper label = new LabelWrapper(new FakeBase())
        LabelWrapper label = new LabelWrapper();
        //FakeLabelWrapper label = new FakeLabelWrapper();
        label.Text = "ToUpper";
        Assert.AreEqual("TOUPPER",label.Text);
        StringWriter stringWriter = new StringWriter();
        HtmlTextWriter writer = new HtmlTextWriter(stringWriter);
        label.RenderControl(writer);
        Assert.AreEqual(1,label.ID);
        Assert.AreEqual("<span>TOUPPER</span>", stringWriter.ToString());
    }
}

public class FakeLabel
{
    virtual public string Text { get; set; }
    public void RenderControl(TextWriter writer)
    {
        writer.Write("<span>" + Text + "</span>");
    }
}

//System Under Test
internal class LabelLogic
{
    internal string ProcessGetText(string value)
    {
        return value.ToUpper();
    }

    internal string ProcessSetText(string value)
    {
        return value.ToUpper();
    }
}
  • 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-05-14T03:42:55+00:00Added an answer on May 14, 2026 at 3:42 am

    This is simply not possible in .Net. You cannot alter compiled meta-data on the fly.

    Think of all of the havoc this would cause. Pretend I had the following situation

    class Example  {
      Label l = new LabelWrapper();
    }
    

    This code has executed and suddenly your code runs that switches the base type of LabelWrapper to be FakeLabel. This raises a number of very hairy problems, including but not limited to

    • What would happen to existing instances of Example?
    • What would happen to new instances of Example as this code is now completely invalid?
    • Number of security nightmares this introduces as I can now mutate my object on the fly in a way APIs can’t account for.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

UPDATE: I've completely changed the question because in reality, I'm having a really hard
UPDATE: I've changed the original question drastically based on Bertrand's suggestions and my own
Update: With iPhone OS 3.0+, the whole UIImagePickerController API has changed. This question and
UPDATE 6/21/12 I have a form in rails that is working similar to an
Update : I found almost exact similar question , yet it has slightly different
UPDATE: I made a mistake in my debugging - this question is not relavent
Update: I am sorry i pasted in list.xml twice by mistake, my question is
Update: Solved, with code I got it working, see my answer below for the
Update: Check out this follow-up question: Gem Update on Windows - is it broken?
Update: Thanks for the suggestions guys. After further research, I’ve reformulated the question here:

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.