Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from appakz/master
Modifications for working with multiple targets
- Loading branch information
Showing
11 changed files
with
362 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace CLAP | ||
{ | ||
public class TargetResolver | ||
{ | ||
private readonly Dictionary<Type, Func<object>> targetHash = new Dictionary<Type, Func<object>>(); | ||
|
||
public TargetResolver() | ||
{ | ||
} | ||
|
||
public TargetResolver(params object[] targets) | ||
{ | ||
foreach (var target in targets) | ||
{ | ||
var innerTarget = target; | ||
RegisterTargetType(innerTarget.GetType(), () => innerTarget); | ||
} | ||
} | ||
|
||
public void RegisterTargetType<T>(Func<T> resolver) | ||
where T: class | ||
{ | ||
RegisterTargetType(typeof (T), () => resolver()); | ||
} | ||
|
||
private void RegisterTargetType(Type targetType, Func<object> resolver) | ||
{ | ||
if (targetHash.ContainsKey(targetType)) | ||
throw new ArgumentException("The provided type is already registered."); | ||
|
||
targetHash.Add(targetType, resolver); | ||
} | ||
|
||
internal Type[] RegisteredTypes { get { return targetHash.Keys.ToArray(); } } | ||
|
||
internal object Resolve(Type targetType) | ||
{ | ||
|
||
if (!targetHash.ContainsKey(targetType)) | ||
{ | ||
throw new ArgumentException("The requested type is not registered."); | ||
} | ||
|
||
return targetHash[targetType](); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.