public static object CreateFromName(string name, params object[] args)
{
if (name == null)
throw new ArgumentNullException(nameof(name));
Type retvalType = null;
// We allow the default table to Types and Strings
// Types get used for types in .Algorithms assembly.
// strings get used for delay-loaded stuff in other assemblies such as .Csp.
object retvalObj;
if (DefaultNameHT.TryGetValue(name, out retvalObj))
{
if (retvalObj is Type)
{
retvalType = (Type)retvalObj;
}
else if (retvalObj is string)
{
retvalType = Type.GetType((string)retvalObj, false, false);
if (retvalType != null && !retvalType.IsVisible)
{
retvalType = null;
}
}
else
{
Debug.Fail("Unsupported Dictionary value:" + retvalObj.ToString());
}
}
// Maybe they gave us a classname.
if (retvalType == null)
{
retvalType = Type.GetType(name, false, false);
if (retvalType != null && !retvalType.IsVisible)
{
retvalType = null;
}
}
// Still null? Then we didn't find it.
if (retvalType == null)
{
return null;
}
// Locate all constructors.
MethodBase[] cons = retvalType.GetConstructors(ConstructorDefault);
if (cons == null)
{
return null;
}
if (args == null)
{
args = new object[] { };
}
List<MethodBase> candidates = new List<MethodBase>();
for (int i = 0; i < cons.Length; i++)
{
MethodBase con = cons[i];
if (con.GetParameters().Length == args.Length)
{
candidates.Add(con);
}
}
if (candidates.Count == 0)
{
return null;
}
cons = candidates.ToArray();
// Bind to matching ctor.
object state;
ConstructorInfo rci = Type.DefaultBinder.BindToMethod(
ConstructorDefault,
cons,
ref args,
null,
null,
null,
out state) as ConstructorInfo;
// Check for ctor we don't like (non-existant, delegate or decorated with declarative linktime demand).
if (rci == null || typeof(Delegate).IsAssignableFrom(rci.DeclaringType))
{
return null;
}
// Ctor invoke and allocation.
object retval = rci.Invoke(ConstructorDefault, Type.DefaultBinder, args, null);
// Reset any parameter re-ordering performed by the binder.
if (state != null)
{
Type.DefaultBinder.ReorderArgumentArray(ref args, state);
}
return retval;
}