Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 29 additions & 20 deletions AFrame.Core/Controls/Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,34 +163,43 @@ public virtual T CreateControl<T>(IEnumerable<SearchProperty> searchProperties)
return control;
}
#endregion

public static T CreateInstance<T>(IContext context, Control parent) where T : Control
public static Control CreateInstance(Type type, IContext context, Control parent)
{
//If type has constructor with 1 parameter and is type IContext. Then use that.
//Else use default constructor.

var type = typeof(T);

//First constructor attempt.
var ctor = type.GetConstructor(new[] { context.GetType() });
if (ctor != null)

Control local;
ConstructorInfo constructor = type.GetConstructor(new Type[] { context.GetType() });
if (constructor != null)
{
local = constructor.Invoke(new object[] { context }) as Control;
if (local == null)
{
throw new Exception("Type is not of type 'Control': " + type.Name);
}
local.Parent = parent;
return local;
}
constructor = type.GetConstructor(Type.EmptyTypes);
if (constructor == null)
{
var ctrl = (T)ctor.Invoke(new object[] { context });
ctrl.Parent = parent;
return ctrl;
throw new Exception("No appropriate constructors found for " + type.Name);
}

//Second constructor attempt.
ctor = type.GetConstructor(Type.EmptyTypes);
if (ctor != null)
local = constructor.Invoke(new object[0]) as Control;
if (local == null)
{
var ctrl = (T)ctor.Invoke(new object[] { });
ctrl.Context = context;
ctrl.Parent = parent;
return ctrl;
throw new Exception("Type is not of type 'Control': " + type.Name);
}
local.Context = context;
local.Parent = parent;
return local;
}

throw new Exception("No appropriate constructors found for " + type.Name);
public static T CreateInstance<T>(IContext context, Control parent) where T : Control
{
var type = typeof(T);
return CreateInstance(type, context, parent) as T;
}

#region Create Controls
Expand Down