1

I am trying to make an instance of a class based on a string that will be retrieved from the User Interface, and then I want to access the properties of the instance of the class.

Here is an overview of what I have so far -

namespace MamdaAdapter
{
    public interface IExchange
    {
        string GetTransport();
    }
}


namespace MamdaAdapter
{
    public class Exchange
    {
        public class Arca : IExchange
        {
            private const string _Transport = "tportname";

            public string GetTransport()
            {
                return _Transport;
            }
        }


        public static IExchange DeriveExchange(string ExchangeName)
        {
            IExchange SelectedExchange = (IExchange)Activator.CreateInstance(Type.GetType(ExchangeName));

            return SelectedExchange;
        }
    }
}



namespace MyUserInterface
{
    public class MainForm
    {
        private void simpleButton1_Click(object sender, EventArgs e)
        {
            IExchange SelectedExchange = Exchange.DeriveExchange("Exchange.Arca");

            Console.WriteLine(SelectedExchange.GetTransport());
        }
    }
}

UPDATE: Right now, I'm getting an Exception that says the "Value cannot be null" which to me means that it is unable to create the instance of the class given the string provided -

4
  • What does not work ? Do you get an error ? Commented Aug 25, 2011 at 18:23
  • What exactly is the problem that you are having? Commented Aug 25, 2011 at 18:24
  • Something else to consider is that there are suites of IoC libraries out there that do this for a living - look at NInject, Unity or others - they can be config driven, programmatically driven etc. Commented Aug 25, 2011 at 18:33
  • Where is that exception being thrown? Could you provide the entire message and the stack dump? Commented Aug 25, 2011 at 19:20

4 Answers 4

2

The problem here is how you specify the name of your class:

First, specify the namespace. Second, since Arca is an inner class you must use '+' instead of '.'

(...) = Exchange.DeriveExchange("MamdaAdapter.Exchange+Arca");
Sign up to request clarification or add additional context in comments.

Comments

2

Assuming you UI doesnt expose the full type name, you typically want a dictionary to associate the display name to the type:

Dictionary<string, Type> _associations = new Dictionary<string, Type>(); 

Then, you simply instantiate the new object:

if(_associations.ContainsKey(someString))
{
   Type selectedType = _associations[someString];

   return Activator.CreateInstance(selectedType) as IExchange;
}

throw new ApplicationException("No type defined for that string yo");

If the string is not known at compile time, you basically need to check for the existance of the type:

var type = Type.GetType(someString);

if(type != null)
{
    // Do Stuff
}

Comments

0

I wrote a small c# console application to simulate your need, tested ok, hope it helps:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MamdaAdapter;
using System.Reflection;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            IExchange SelectedExchange = Exchange.DeriveExchange("MamdaAdapter.Arca");
            Console.WriteLine(SelectedExchange.GetTransport());
        }
    }
}

namespace MamdaAdapter
{
    public interface IExchange
    {
        string GetTransport();
    }
}


namespace MamdaAdapter
{
    public class Arca : IExchange
    {
        private const string _Transport = "tportname";

        public string GetTransport()
        {
            return _Transport;
        }
    }
}

namespace MamdaAdapter
{
    public class Exchange
    {
        public static IExchange DeriveExchange(string ExchangeName)
        {
            IExchange SelectedExchange = (IExchange)Assembly.GetAssembly(typeof(IExchange)).CreateInstance(ExchangeName, false, BindingFlags.CreateInstance, null, null, null, null);
            return SelectedExchange;
        }
    }

}

Comments

0

If the Type you are looking for is not defined in the same assembly that is executing Type.GetType you must use the AssemblyQualifiedName (something like MyNamespace.MyClass, MyAssembly, Version=1.3.0.0, Culture=neutral, PublicKeyToken=b17a5c561934e089), even the FullName is not enough. Otherwise you could first get the assembly containing the class and then execute the GetType method of the Assembly class.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.