1

I'm trying to create instances of objects that have the same base type like this:

  Assembly myAssembly = Assembly.GetExecutingAssembly();
  var objs = myAssembly.GetTypes().Where(t => t.BaseType == typeof(SearchLogicObjectBase)).OrderBy(o => o.FullName);

  foreach (var item in objs)
  {
    SearchLogicObjectBase p = (SearchLogicObjectBase)Activator.CreateInstance(item.GetType(), new Object[] { false });        
    _searchlogic.AddDefaultSearchObject(p);
  }

The derived objects have constructors like this:

public SearchLogicCsri()
  : this(true)
{ }

public SearchLogicCsri(bool extendsearch)
  : base(extendsearch)
{
  Table = "csri";
  ViewModel = "CsriViewModel";
  ExtendSearch = extendsearch;
}

and the base object (SearchLogicObjectBase) has a constructor like this:

public SearchLogicObjectBase(bool extendsearch)
{
  _extendsearch = extendsearch;
}

However, the above code results in the following exception:

Constructor on type 'System.RuntimeType' not found.

Both the base and derived types have a constructor that takes a single boolean so I'm not sure why I get the error.

Can anyone help please?

0

1 Answer 1

7

Do not pass item.GetType() but item, since item is already a type.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.