annotate datagear/dg/DataGear.cs @ 3:f27db91a852e

update MessagePackDagaGear.cs
author riono <e165729@ie.u-ryukyu.ac.jp>
date Tue, 23 Jun 2020 16:03:32 +0900
parents 740b80680d75
children e6f5b7d14dd1
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
1
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
1 using System;
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
2
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
3 namespace Christie_net.datagear.dg {
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
4 public class DataGear<T> {
3
f27db91a852e update MessagePackDagaGear.cs
riono <e165729@ie.u-ryukyu.ac.jp>
parents: 1
diff changeset
5 protected T data;
1
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
6 protected Type clazz = null;
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
7
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
8 public DataGear(T data) {
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
9 SetClazz(data.GetType());
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
10 SetData(data);
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
11 }
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
12
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
13 public DataGear(Type clazz) {
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
14 SetClazz(clazz);
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
15 }
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
16
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
17 public void SetData(T data) {
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
18 Type dataClazz = data.GetType();
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
19
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
20 if (dataClazz == this.clazz) {
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
21 this.data = data;
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
22 return;
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
23 }
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
24
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
25 Type dataSuperClazz = dataClazz.BaseType;
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
26 while (dataSuperClazz != null) {
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
27 if (dataClazz == this.clazz) {
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
28 this.data = data;
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
29 return;
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
30 }
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
31 dataSuperClazz = dataSuperClazz.BaseType;
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
32 }
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
33
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
34 Type[] interfaces = dataClazz.GetInterfaces();
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
35 foreach (Type interfaze in interfaces) {
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
36 if (interfaze == this.clazz) {
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
37 this.data = data;
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
38 return;
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
39 }
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
40 }
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
41
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
42 throw new InvalidCastException("datagear cannot set class from" + dataClazz.GetType().Name + " to " + data.GetType().Name);
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
43 }
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
44
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
45 public void SetClazz(Type clazz) {
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
46 this.clazz = clazz;
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
47 }
3
f27db91a852e update MessagePackDagaGear.cs
riono <e165729@ie.u-ryukyu.ac.jp>
parents: 1
diff changeset
48
f27db91a852e update MessagePackDagaGear.cs
riono <e165729@ie.u-ryukyu.ac.jp>
parents: 1
diff changeset
49 public Type GetClazz() {
f27db91a852e update MessagePackDagaGear.cs
riono <e165729@ie.u-ryukyu.ac.jp>
parents: 1
diff changeset
50 return clazz;
f27db91a852e update MessagePackDagaGear.cs
riono <e165729@ie.u-ryukyu.ac.jp>
parents: 1
diff changeset
51 }
f27db91a852e update MessagePackDagaGear.cs
riono <e165729@ie.u-ryukyu.ac.jp>
parents: 1
diff changeset
52
f27db91a852e update MessagePackDagaGear.cs
riono <e165729@ie.u-ryukyu.ac.jp>
parents: 1
diff changeset
53 public T GetData() {
f27db91a852e update MessagePackDagaGear.cs
riono <e165729@ie.u-ryukyu.ac.jp>
parents: 1
diff changeset
54 return data;
f27db91a852e update MessagePackDagaGear.cs
riono <e165729@ie.u-ryukyu.ac.jp>
parents: 1
diff changeset
55 }
f27db91a852e update MessagePackDagaGear.cs
riono <e165729@ie.u-ryukyu.ac.jp>
parents: 1
diff changeset
56
f27db91a852e update MessagePackDagaGear.cs
riono <e165729@ie.u-ryukyu.ac.jp>
parents: 1
diff changeset
57 public override string ToString() {
f27db91a852e update MessagePackDagaGear.cs
riono <e165729@ie.u-ryukyu.ac.jp>
parents: 1
diff changeset
58 return "DataGear : classname " + clazz;
f27db91a852e update MessagePackDagaGear.cs
riono <e165729@ie.u-ryukyu.ac.jp>
parents: 1
diff changeset
59 }
1
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
60 }
740b80680d75 add Codegear and Datagear files
riono <e165729@ie.u-ryukyu.ac.jp>
parents:
diff changeset
61 }