13

I'm reading information from an XML which contains the type of an object that I need to instantiate along with it's constructor parameters.

The object type is actually in another project, within a sibling namespace. (I need to create a Company.Project2.Type within Company.Project1 class.)

I found this question, but it doesn't handle the constructor parameters or the fact that it's in another namespace.

How can I do this?

Edit: The assembly name and default namespace wasn't set correctly in the project properties.

Community
  • 1
  • 1
Ben S
  • 68,394
  • 30
  • 171
  • 212

2 Answers2

33
  • You need to specify the full type name to Type.GetType(), including namespace, e.g. "Company.Project2.Type"
  • If the type isn't in the same assembly (or mscorlib), you need to give the assembly name too, including version information if it's strongly typed. For example, for a non-strongly typed assembly Company.Project2.dll, you might specify "Company.Project2.Type, Company.Project2".
  • To call a constructor with parameters you can call Activator.CreateInstance(Type, Object[]) or get the exact constructor you want with Type.GetConstructor() and then call ConstructorInfo.Invoke().

If that doesn't help, please give more information.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Type.GetType() doesn't seem to find the type with the string I give it. The class Prop is defined in the Reaver.GamePlay namespace, but the returned type is null. – Ben S Mar 15 '09 at 17:27
  • I've also tried "Reaver.GamePlay.Prop, Reaver.GamePlay" to no avail. – Ben S Mar 15 '09 at 17:32
  • @Ben: What assembly is it in? Is the assembly strongly typed? – Jon Skeet Mar 15 '09 at 17:59
  • @JonSkeet I believe it is impossible to use this code to generates String object - since it doesnt have a constructor.(IMHO) .... Can you please provide an alternative for string ? – Royi Namir Aug 03 '12 at 16:03
  • @RoyiNamir: There are various constructors for `string`. Have a look at MSDN. But in the case of string, it would probably be worth special-casing to just use the string data directly. – Jon Skeet Aug 03 '12 at 16:13
  • @JonSkeet I meant Default CTOR. ( sorry for not mention it) – Royi Namir Aug 03 '12 at 16:34
  • @RoyiNamir: Right, there's no parameterless constructor. But this question states: "I'm reading information from an XML which contains the type of an object that I need to instantiate along with it's constructor parameters." – Jon Skeet Aug 03 '12 at 16:35
3

If you want to create a type dynamically at run time, Activator.CreateInstance Method will do it for you. If you issue is with the type having a constructor with parameters, this overload will do this. For example, http://msdn.microsoft.com/en-us/library/wcxyzt4d.aspx

I advise looking through the overloads for the best match.

The namespace issue should be not be relavant - as long as the dll is in the bin directory or the GAC you should be OK. The rules may change if the assembly is Strongly named though.

Could you provide a code snippiet of the code that is not working (using the method you linked to)? This + the errors you are seeing will be very helpful! [update] Quick sample using the Activator.CreateInstance that handles Constructors w/ paramaters.

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

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            BaseProduct b =(BaseProduct)System.Activator.CreateInstance(Type.GetType("ConsoleApplication1.Product")
                ,new object[]{typeof(string)}, 
                new object[]{"123"}
            );
            //Activator..::.CreateInstance Method (Type, array<Object>[]()[], array<Object>[]()[])
        }
    }
    public class Product: BaseProduct{
        public  Product(string id) { 

        }
        public string Id {get;set;}


   }

    public abstract class BaseProduct {
        abstract public string Id { get; set; }
    }
}
Community
  • 1
  • 1
brian chandley
  • 1,276
  • 1
  • 10
  • 20