StyleCop clean OjbectCreator

This commit is contained in:
Matthias Mailänder
2013-08-04 09:04:36 +02:00
parent 32cbe4620f
commit 2653f95853

View File

@@ -19,9 +19,9 @@ namespace OpenRA
{
public class ObjectCreator
{
Pair<Assembly, string>[] ModAssemblies;
Pair<Assembly, string>[] modAssemblies;
public ObjectCreator( Manifest manifest )
public ObjectCreator(Manifest manifest)
{
// All the core namespaces
var asms = typeof(Game).Assembly.GetNamespaces()
@@ -35,7 +35,7 @@ namespace OpenRA
asms.AddRange(asm.GetNamespaces().Select(ns => Pair.New(asm, ns)));
}
ModAssemblies = asms.ToArray();
modAssemblies = asms.ToArray();
}
public static Action<string> MissingTypeAction =
@@ -43,57 +43,59 @@ namespace OpenRA
public T CreateObject<T>(string className)
{
return CreateObject<T>( className, new Dictionary<string, object>() );
return CreateObject<T>(className, new Dictionary<string, object>());
}
public T CreateObject<T>( string className, Dictionary<string, object> args )
public T CreateObject<T>(string className, Dictionary<string, object> args)
{
foreach( var mod in ModAssemblies )
foreach (var mod in modAssemblies)
{
var type = mod.First.GetType( mod.Second + "." + className, false );
if( type == null ) continue;
var type = mod.First.GetType(mod.Second + "." + className, false);
if (type == null) continue;
var flags = BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance;
var ctors = type.GetConstructors( flags )
.Where( x => x.HasAttribute<UseCtorAttribute>() ).ToList();
var ctors = type.GetConstructors(flags)
.Where(x => x.HasAttribute<UseCtorAttribute>()).ToList();
if( ctors.Count == 0 )
return (T)CreateBasic( type );
else if( ctors.Count == 1 )
return (T)CreateUsingArgs( ctors[ 0 ], args );
if (ctors.Count == 0)
return (T)CreateBasic(type);
else if (ctors.Count == 1)
return (T)CreateUsingArgs(ctors[0], args);
else
throw new InvalidOperationException( "ObjectCreator: UseCtor on multiple constructors; invalid." );
throw new InvalidOperationException("ObjectCreator: UseCtor on multiple constructors; invalid.");
}
MissingTypeAction(className);
return default(T);
}
public object CreateBasic( Type type )
public object CreateBasic(Type type)
{
return type.GetConstructor( new Type[ 0 ] ).Invoke( new object[ 0 ] );
return type.GetConstructor(new Type[0]).Invoke(new object[0]);
}
public object CreateUsingArgs( ConstructorInfo ctor, Dictionary<string, object> args )
public object CreateUsingArgs(ConstructorInfo ctor, Dictionary<string, object> args)
{
var p = ctor.GetParameters();
var a = new object[ p.Length ];
for( int i = 0 ; i < p.Length ; i++ )
var a = new object[p.Length];
for (int i = 0; i < p.Length; i++)
{
var key = p[i].Name;
if ( !args.ContainsKey(key) ) throw new InvalidOperationException("ObjectCreator: key `{0}' not found".F(key));
a[ i ] = args[ key ];
if (!args.ContainsKey(key)) throw new InvalidOperationException("ObjectCreator: key `{0}' not found".F(key));
a[i] = args[key];
}
return ctor.Invoke( a );
return ctor.Invoke(a);
}
public IEnumerable<Type> GetTypesImplementing<T>()
{
var it = typeof(T);
return ModAssemblies.Select( ma => ma.First ).Distinct()
return modAssemblies.Select(ma => ma.First).Distinct()
.SelectMany(ma => ma.GetTypes()
.Where(t => t != it && it.IsAssignableFrom(t)));
}
[AttributeUsage( AttributeTargets.Constructor )]
public class UseCtorAttribute : Attribute {}
[AttributeUsage(AttributeTargets.Constructor)]
public class UseCtorAttribute : Attribute { }
}
}