2 using System.Collections.Generic;
5 using System.Reflection;
6 using System.Collections;
8 using System.Xml.Serialization;
9 using DirectOutput.General.Generic;
11 namespace DocumentationHelper
21 set { _Type = value; }
29 string N =
"{0}.{1}".Build(
Type.Namespace,
Type.Name);
30 if (VSXmlDocu.TypeSummary.ContainsKey(N))
32 return VSXmlDocu.TypeSummary[N];
45 S +=
"\\section use_{0}_{1} {1}\n\n".Build(
NamespaceName.Replace(
".",
"_"),
Name);
47 if (!
Summary.IsNullOrWhiteSpace())
49 S +=
"\\subsection use_{0}_{1}_summary Summary\n\n".Build(
NamespaceName.Replace(
".",
"_"),
Name);
55 if (!Xml.IsNullOrWhiteSpace())
57 S +=
"\\subsection use_{0}_{1}_samplexml Sample XML\n\n".Build(
NamespaceName.Replace(
".",
"_"),
Name);
58 S +=
"A configuration section for {0} might resemble the following structure:\n\n".Build(
Name);
59 S +=
"~~~~~~~~~~~~~{.xml}\n";
61 S +=
"\n~~~~~~~~~~~~~\n";
65 PDL.Sort((PDD1, PDD2) => PDD1.Name.CompareTo(PDD2.Name));
68 S +=
"\\subsection use_{0}_{1}_properties Properties\n\n".Build(
NamespaceName.Replace(
".",
"_"),
Name);
69 S +=
"{0} has the following {1} configurable properties:\n\n".Build(
Name, PDL.Count);
86 public string Name {
get {
return Type.Name; } }
95 object O = GetSampleReferenceType(
Type);
100 using (MemoryStream ms =
new MemoryStream())
103 XmlSerializerNamespaces Namespaces =
new XmlSerializerNamespaces();
104 Namespaces.Add(
string.Empty,
string.Empty);
105 new XmlSerializer(
Type).Serialize(ms, O, Namespaces);
107 using (StreamReader sr =
new StreamReader(ms, Encoding.Default))
109 Xml = sr.ReadToEnd();
114 Xml = Xml.Substring(Xml.IndexOf(
"\n")+1);
124 List<PropertyDocuData> L =
new List<PropertyDocuData>();
125 foreach (PropertyInfo PI
in Type.GetXMLSerializableProperties())
139 private object GetSampleReferenceType(
Type RefType)
144 DirectOutput.General.TypeList Types =
new DirectOutput.General.TypeList(AppDomain.CurrentDomain.GetAssemblies().ToList().SelectMany(s => s.GetTypes()).Where(p => T.IsAssignableFrom(p) && !p.IsAbstract));
156 Object O = Activator.CreateInstance(T);
159 foreach (PropertyInfo PI
in O.GetType().GetXMLSerializableProperties())
161 if (PI.PropertyType == typeof(
string))
163 string V = (string)PI.GetValue(O, null);
164 if (V.IsNullOrWhiteSpace())
166 if (PI.Name.ToUpper() ==
"NAME")
168 PI.SetValue(O,
"Name of {0}".Build(O.GetType().Name), null);
170 else if (PI.Name.ToUpper().EndsWith(
"NAME"))
172 PI.SetValue(O,
"Name of {0}".Build(PI.Name.Substring(0, PI.Name.Length - 4)), null);
176 PI.SetValue(O,
"{0} string".Build(PI.Name), null);
180 else if (PI.PropertyType.IsArray)
182 object A = PI.GetValue(O,null);
185 object[] SampleArray = GetSampleArray(PI.PropertyType);
186 PI.SetValue(O, SampleArray, null);
189 else if (PI.PropertyType.IsGenericList())
192 IList EN = GetSampleGenericList(PI.PropertyType);
193 PI.SetValue(O, EN, null);
195 else if (typeof(IDictionary).IsAssignableFrom(PI.PropertyType) && PI.PropertyType.IsGenericType)
197 IDictionary EN = GetSampleGenericDictionary(PI.PropertyType);
198 PI.SetValue(O, EN, null);
200 else if (PI.PropertyType.IsValueType)
202 if (PI.PropertyType == typeof(
char))
204 PI.SetValue(O, GetSampleValueType(PI.PropertyType), null);
207 else if (PI.PropertyType == typeof(DateTime))
209 PI.SetValue(O, DateTime.Now, null);
211 else if (PI.PropertyType == typeof(TimeSpan))
213 PI.SetValue(O, TimeSpan.Zero, null);
215 else if (!PI.PropertyType.IsValueType)
217 PI.SetValue(O, GetSampleReferenceType(PI.PropertyType), null);
221 throw new Exception(
"Unknow type {0}".Build(PI.PropertyType.Name));
229 private object[] GetSampleArray(System.Type type)
234 object[] A = (
object[])Array.CreateInstance(typeof(
object),3);
236 Type GT = type.GetElementType();
238 for (
int i = 0; i < 3; i++)
243 G = GetSampleValueType(GT);
247 G = GetSampleReferenceType(GT);
261 private IDictionary GetSampleGenericDictionary(System.Type T)
263 if (!(typeof(IDictionary).IsAssignableFrom(T) && T.IsGenericType))
return null;
264 IDictionary C = (IDictionary)Activator.CreateInstance(T);
265 if (T.GetGenericArguments().Length == 2)
267 Type KT = T.GetGenericArguments()[0];
268 Type VT = T.GetGenericArguments()[1];
271 for (
int i = 0; i < 3; i++)
273 if (KT == typeof(
string))
275 K =
"Key {0}".Build(i);
277 else if (KT.IsValueType)
279 K = GetSampleValueType(KT);
282 K = Convert.ChangeType(i, KT);
287 K = GetSampleReferenceType(KT);
289 if (VT == typeof(
string))
291 V =
"Value {0}".Build(i);
293 else if (KT.IsValueType)
295 V = GetSampleValueType(KT);
298 K = Convert.ChangeType(i, KT);
303 V = GetSampleReferenceType(KT);
316 private IList GetSampleGenericList(
Type T)
318 if (!(typeof(IList).IsAssignableFrom(T) ))
return null;
319 IList C = (IList)Activator.CreateInstance(T);
324 Type GT = T.GetGetGenericCollectionTypeArguments()[0];
326 for (
int i = 0; i < 3; i++)
331 G = GetSampleValueType(GT);
335 G = GetSampleReferenceType(GT);
344 private object GetSampleValueType(
Type T)
346 if (!T.IsValueType)
return null;
348 object O = Activator.CreateInstance(T);
349 if (T == typeof(
char))