forked from nanoframework/Samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
61 lines (50 loc) · 1.9 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//
// Copyright (c) .NET Foundation and Contributors
// See LICENSE file in the project root for full license information.
//
using System.Diagnostics;
using Microsoft.Extensions.DependencyInjection;
namespace nanoFramework.Simple
{
public class Program
{
public static void Main()
{
// registering services
var serviceProvider = new ServiceCollection()
.AddSingleton(typeof(ServiceObject))
.AddSingleton(typeof(RootObject))
.BuildServiceProvider();
// create a service provider to get access to the RootObject
var service = (RootObject)serviceProvider.GetService(typeof(RootObject));
service.ServiceObject.Three = "3";
// create an updated instance of the root object
var instance = (RootObject)ActivatorUtilities.CreateInstance(serviceProvider, typeof(RootObject), 1, "2");
Debug.WriteLine($"One: {instance.One}");
Debug.WriteLine($"Two: {instance.Two}");
Debug.WriteLine($"Three: {instance.ServiceObject.Three}");
Debug.WriteLine($"Name: {instance.ServiceObject.GetType().Name}");
}
public class ServiceObject
{
public string Three { get; set; }
}
public class RootObject
{
public int One { get; }
public string Two { get; }
public ServiceObject ServiceObject { get; protected set; }
public RootObject(ServiceObject serviceObject)
{
ServiceObject = serviceObject;
}
// constructor with the most parameters will be used for activation
public RootObject(ServiceObject serviceObject, int one, string two)
{
ServiceObject = serviceObject;
One = one;
Two = two;
}
}
}
}