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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T05:27:59+00:00 2026-06-01T05:27:59+00:00

I am trying to add some specific modular functonality to my application, I need

  • 0

I am trying to add some specific modular functonality to my application, I need users to be able to create their own classes and then have them imported into the program at runtime, these classes will follow a specific template so I can call functions within their new class.

For example, a class could be: http://pastebin.com/90NTjia9

I would compile the class and then execute doSomething();

How would I achieve this in C#?

  • 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-01T05:28:01+00:00Added an answer on June 1, 2026 at 5:28 am

    If your users have the necessary tools (such as Visual Studio, which they really should have if they’re writing C# classes), they can supply you with a DLL, which you can then load dynamically:

    private static T CreateInstance(string assemblyName, string typeName)
    {
        var assembly = Assembly.LoadFrom(assemblyName);
    
        if (assembly == null)
            throw new InvalidOperationException(
               "The specified assembly '" + assemblyName + "' did not load.");
    
        Type type = assembly.GetType(typeName);
        if (type == null)
            throw new InvalidOperationException(
               "The specified type '" + typeName + "' was not found in assembly '" + assemblyName + "'");
    
        return (T)Activator.CreateInstance(type);
    }
    

    The T generic parameter is there so that you can pass an abstract class or interface to cast the type instance to:

    public interface IDoSomething
    {
        bool DoSomething();
    }
    

    Your users inherit from this interface when they write their own class:

    public class UserDefinedClass : IDoSomething
    {
        public bool DoSomething()
        {
            // Implementation here.
        }
    }
    

    This allows you to retain type safety and call the class methods directly, rather than relying on Reflection to do so.

    If you really want your users to provide C# source, you can compile their class at runtime like this:

    private Assembly BuildAssembly(string code)
    {
        var provider = new CSharpCodeProvider();
        var compiler = provider.CreateCompiler();
        var compilerparams = new CompilerParameters();
        compilerparams.GenerateExecutable = false;
        compilerparams.GenerateInMemory = true;
        var results = compiler.CompileAssemblyFromSource(compilerparams, code);
        if (results.Errors.HasErrors)
        {
            var errors = new StringBuilder("Compiler Errors :\r\n");
            foreach (CompilerError error in results.Errors )
            {
                errors.AppendFormat("Line {0},{1}\t: {2}\n", 
                       error.Line, error.Column, error.ErrorText);
            }
            throw new Exception(errors.ToString());
        }
        else
        {
            return results.CompiledAssembly;
        }
    }
    

    Then, you just substitute the resulting assembly in the CreateInstance code above, instead of the externally-loaded assembly. Note that your users will still need to provide the appropriate using statements at the top of their class.

    There are also places on the Internet that explain how to get an Eval() function in C#, in case you don’t really need a full-blown class.

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

Sidebar

Related Questions

I'm trying to add some accessibility for screen readers into a Flash application, and
I'm trying to add some jQuery + ERB to a specific view: views/posts/show.html.erb (top
I was trying to add some buttons to some specific positions on an image.
I am trying to add some html after a specific h1 tag. The line
I was trying to add the data i receive from some specific messages to
Im trying to add some short file names to a Wise installer to fix
I'm trying to add some security to my ASP.NET 1.0 MVC app (VB), but
So I'm trying to add some ability to my project to allow user-defined properties
I'm trying to add some twitter funcionality to my website (php). I publish links
I am trying to add some additional logic to the 'Promoted to front page'

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.