-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeMapper.cs
More file actions
110 lines (103 loc) · 2.92 KB
/
TypeMapper.cs
File metadata and controls
110 lines (103 loc) · 2.92 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
using Simple.BasicNet.Core.Atrributes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
/*********************************************************
* 命名空间 Simple.BasicNet.Core.Container
* 接口名称 TypeMapping
* 开发人员:11920
* 创建时间:2023/4/6 13:02:08
* 描述说明:
* 更改历史:
*
* *******************************************************/
namespace Simple.BasicNet.Core
{
internal class TypeMapper:ITypeMapper
{
public TypeMapper(Type original,Type target,eLifeCycle lifeCycle)
{
Original= original;
Target = new Dictionary<string,Tuple<Type, object>>();
AddTarget(target);
LifeCycle= lifeCycle;
IsAutowired = false;
AutowiredType = typeof(AutowiredAttribute);
}
public TypeMapper(Type original,Type target):this(original, target, eLifeCycle.Instant)
{
}
public TypeMapper(Type target,eLifeCycle lifeCycle):this(target,target,lifeCycle)
{
}
public TypeMapper(Type target) : this(target,eLifeCycle.Instant)
{
}
public Type Original { get { return original; } set { original = value; } }
public Dictionary<string,Tuple<Type,object>> Target { get { return target; } set { target = value; } }
public eLifeCycle LifeCycle { get { return lifeCycle; } set { lifeCycle = value; } }
public string TargetKey { get {return targetKey; } set { targetKey = value; } }
public bool IsAutowired { get {return isAutowired; } set { isAutowired = value; } }
public Type AutowiredType { get {return autowiredType; } set { autowiredType = value; } }
private Type original;
private Dictionary<string,Tuple<Type,object>> target;
private eLifeCycle lifeCycle;
private string targetKey;
private bool isAutowired;
private Type autowiredType;
public void AddTarget(Type target)
{
if (!Target.ContainsKey(target.FullName))
{
TargetKey= target.FullName;
Target.Add(TargetKey,Tuple.Create<Type,object>(target,null));
}
}
public string GetTypeMapperKey()
{
return $"{Original.FullName}";
}
public void Verify()
{
if (!GetTargetType(TargetKey).IsAssignableTo(Original)
&& !GetTargetType(TargetKey).GetInterfaces().Any(t => t.FullName == Original.FullName))
throw new Exception("注册失败,TTarget类型必须继承或者实现Original类型!");
}
public Type GetTargetType(string key)
{
if (Target.ContainsKey(key))
{
return Target[key].Item1;
}
return default(Type);
}
public object GetInstance(string key)
{
if (Target.ContainsKey(key))
{
return Target[key].Item2;
}
return default(object);
}
public void SetInstance(string key,object target)
{
if (Target.ContainsKey(key))
{
Target[key]=Tuple.Create<Type,object>(target.GetType(),target);
}
}
#region
public void Autowired()
{
this.isAutowired= true;
}
public void Autowired(Type type)
{
autowiredType=type;
Autowired();
}
#endregion
}
}