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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T20:43:45+00:00 2026-06-04T20:43:45+00:00

Say that I have a webpage called myPage which implement Page, but also implement

  • 0

Say that I have a webpage called myPage which implement Page, but also implement my own interface called myInterface. My objective is to call a function called myFunction in myInterface with just the name of the class in string.

public interface MyInterfac{
          Myfunction();
    }
public partial class MyPage1: Page, MyInterface{ 
          Myfunction(){ return "AAA"; }
    }
public partial class MyPage2: Page, MyInterface{ 
          Myfunction(){ return "BBB"; }
    }

Now here is what information I have available to me:

    string pageName1 = "MyPage1";
    string pageName2 = "MyPage2";

How do get from here to something along the line of:

   (MyInterface)MyPage1_instance.Myfunction();         //Should return AAA;
   (MyInterface)MyPage2_instance.Myfunction();         //Should return BBB;

EDIT: Here is when I try to create an instance of MyInterface and it does not work:

Type myTypeObj = Type.GetType("MyPage1");
MyInterface MyPage1_instance = (MyInterface) Activator.CreateInstance (myTypeObj);
  • 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-04T20:43:47+00:00Added an answer on June 4, 2026 at 8:43 pm

    If you’re looking for information that doesn’t change from one instance of your type to the next, you should probably use an attribute. Here is an example:

    [System.ComponentModel.Description("aaa")]
    class Page1 { }
    
    [System.ComponentModel.Description("bbb")]
    class Page2 { }
    
    [TestClass]
    public class Tests
    {
        private static string GetDescription(string typeName)
        {
            var type = System.Reflection.Assembly.GetExecutingAssembly()
                .GetTypes().Single(t => t.Name == typeName);
    
            return type.GetCustomAttributes(false)
                .OfType<System.ComponentModel.DescriptionAttribute>()
                .Single().Description;
        }
    
        [TestMethod]
        public void MyTestMethod()
        {
            Assert.AreEqual("aaa", GetDescription("Page1"));
            Assert.AreEqual("bbb", GetDescription("Page2"));
        }
    }
    

    A couple of notes

    • Look around in System.ComponentModel to see if there is already an appropriate attribute for what you’re trying to do. I used the Description attribute for this example. If you can’t find a good one, create your own custom attribute.
    • It would probably be better to use Type.GetType("Qualified.Name.Of.Page1") if you have that information available.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

say that i have a project which lies in a folder called 'bin', and
I am creating a webpage..as usual. But I have a problem. Lets say that
I have to do a asp.net web page that when load will say something
Say that I have an article with multiple pages. Each page has a short
Lets say that you have either URL or a link on a webpage that
I have a webpage that fires off an Ajax request which stores some data
Let's say you have a view called View Story which is just a web
Let's say that I have a link to a webpage that contains some text.
Let's say I have a static webpage and the whole page is wrapped, let's
Say that i have a generic dictionary with data like this (I hope the

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.