Program.cs
155 lines
| 4.2 KiB
| text/x-csharp
|
CSharpLexer
/ Implab.Playground / Program.cs
cin
|
r267 | using System; | ||
cin
|
r277 | using System.Collections.Generic; | ||
cin
|
r268 | using System.Diagnostics; | ||
cin
|
r284 | using System.Dynamic; | ||
cin
|
r272 | using System.Linq; | ||
cin
|
r278 | using Implab.Components; | ||
cin
|
r268 | using Implab.Diagnostics; | ||
cin
|
r267 | using Implab.ServiceHost.Unity; | ||
cin
|
r229 | using Implab.Xml; | ||
cin
|
r267 | using Unity; | ||
using Unity.Injection; | ||||
cin
|
r272 | using Unity.Registration; | ||
cin
|
r229 | |||
namespace Implab.Playground { | ||||
cin
|
r267 | |||
public class Foo { | ||||
cin
|
r269 | |||
public class Bar { | ||||
} | ||||
cin
|
r270 | public string Name { get; set; } | ||
cin
|
r267 | public int IntValue { get; set; } | ||
public string StringValue { get; set; } | ||||
cin
|
r277 | public void AddRange(Foo[] items) { | ||
Console.WriteLine($"AddRange: Foo[]"); | ||||
} | ||||
cin
|
r267 | } | ||
cin
|
r278 | public class FooFactory : IFactory<Foo>, IFactory<Foo.Bar> { | ||
public bool UseSsl { get; set; } | ||||
public string Connection { get; set; } | ||||
public Foo Create() { | ||||
return new Foo() { | ||||
Name = "AutoFac" | ||||
}; | ||||
} | ||||
Foo.Bar IFactory<Foo.Bar>.Create() { | ||||
return new Foo.Bar(); | ||||
} | ||||
} | ||||
cin
|
r272 | public interface IContainer<T> { | ||
T Instance { get; set; } | ||||
} | ||||
public class Container<T> : IContainer<T> { | ||||
cin
|
r278 | public class Bar { | ||
} | ||||
public class Bar<T2> { | ||||
public class Baz { | ||||
} | ||||
} | ||||
cin
|
r267 | public Container() { | ||
} | ||||
public Container(T instance) { | ||||
Instance = instance; | ||||
} | ||||
public T Instance { get; set; } | ||||
cin
|
r270 | |||
public void SetInstance(T value) { | ||||
Instance = value; | ||||
} | ||||
cin
|
r277 | |||
public void AddRange(List<T> items) { | ||||
Console.WriteLine($"AddRange: {typeof(List<T>)}"); | ||||
} | ||||
public void AddRange(T[] items) { | ||||
Console.WriteLine($"AddRange: T[] ofType {typeof(T[])}"); | ||||
} | ||||
cin
|
r267 | } | ||
cin
|
r255 | |||
cin
|
r229 | public class Program { | ||
cin
|
r255 | static void Main(string[] args) { | ||
cin
|
r280 | var u1 = new Uri("/some/one"); | ||
cin
|
r284 | dynamic obj = new ExpandoObject(); | ||
obj.Name = "Dynamo"; | ||||
cin
|
r280 | |||
cin
|
r284 | obj.Hello = new Func<string>(() => { return "Hello"; }); | ||
cin
|
r280 | |||
cin
|
r284 | Console.WriteLine($"{obj.Hello()}"); | ||
cin
|
r280 | |||
} | ||||
static void Main2(string[] args) { | ||||
cin
|
r277 | var listener = new SimpleTraceListener(Console.Out); | ||
var source = Trace<TypeResolver>.TraceSource; | ||||
source.Switch.Level = SourceLevels.All; | ||||
source.Listeners.Add(listener); | ||||
cin
|
r279 | var stopwatch = new Stopwatch(); | ||
stopwatch.Start(); | ||||
var container = new UnityContainer(); | ||||
Console.WriteLine($"Created: {stopwatch.ElapsedMilliseconds}"); | ||||
stopwatch.Restart(); | ||||
container.LoadXmlConfiguration("data/sample.xml"); | ||||
cin
|
r273 | |||
cin
|
r279 | Console.WriteLine($"Loaded: {stopwatch.ElapsedMilliseconds}"); | ||
cin
|
r273 | |||
cin
|
r279 | stopwatch.Restart(); | ||
var instace1 = container.Resolve<IContainer<string>>(); | ||||
Console.WriteLine($"Resolved1: {stopwatch.ElapsedMilliseconds}"); | ||||
cin
|
r277 | |||
cin
|
r279 | stopwatch.Restart(); | ||
var instace2 = container.Resolve<IContainer<Foo>>(); | ||||
Console.WriteLine($"Resolved2: {stopwatch.ElapsedMilliseconds}"); | ||||
DisplayContainerRegistrations(container); | ||||
cin
|
r272 | } | ||
cin
|
r268 | |||
cin
|
r272 | static void DisplayContainerRegistrations(IUnityContainer theContainer) { | ||
string regName, regType, mapTo, lifetime; | ||||
Console.WriteLine("Container has {0} Registrations:", | ||||
theContainer.Registrations.Count()); | ||||
foreach (ContainerRegistration item in theContainer.Registrations) { | ||||
regType = item.RegisteredType.FullName; | ||||
mapTo = item.MappedToType.FullName; | ||||
regName = item.Name ?? "[default]"; | ||||
lifetime = item.LifetimeManager.LifetimeType.Name; | ||||
if (mapTo != regType) { | ||||
mapTo = " -> " + mapTo; | ||||
} else { | ||||
mapTo = string.Empty; | ||||
} | ||||
lifetime = lifetime.Substring(0, lifetime.Length - "LifetimeManager".Length); | ||||
Console.WriteLine("+ {0}{1} '{2}' {3}", regType, mapTo, regName, lifetime); | ||||
} | ||||
cin
|
r233 | } | ||
cin
|
r229 | |||
} | ||||
} | ||||