代码改变世界

Generate a Class

2012-03-31 21:29  Oliver_Zhao  阅读(329)  评论(0编辑  收藏  举报

我们知道有很多类很有用,但不知道如何对它进行初始化,这时我们应该怎么办?

TextReader

TextReader Class: Represents a reader that can read a sequential series of characters.
System.Object
  -System.MarshalByRefObject
     -System.IO.TextReader
       *System.IO.StreamReader
       *System.IO.StringReader 
我们发现TextReader有两个子类,分别是System.IO.StreamReader和System.IO.StringReader。这时侯我们可以想到可以将子类对象赋值给父类对象。复习一下,为什么可以这样做?因为子类对象在初始化的时候会将父类的所有对象初始化,这样,将子类对象赋值给父类对象就是ok的。
string path = @"D:\ss.txt";
if (File.Exists(path))
{
TextReader textReader = File.OpenText(path);
Console.WriteLine(textReader.ReadToEnd());
}
 

XDocuments

XDocuments Class: Represents an XML document.

我们从他的类的继承关系中可以看到,XDocument是一个最为底层的类。我们接下来看下它的metaData。
View Code
  1     // 摘要:
2 // 表示 XML 文档。
3 public class XDocument : XContainer
4 {
5 // 摘要:
6 // 初始化 System.Xml.Linq.XDocument 类的新实例。
7 [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
8 public XDocument();
9 //
10 // 摘要:
11 // 使用指定的内容初始化 System.Xml.Linq.XDocument 类的新实例。
12 //
13 // 参数:
14 // content:
15 // 要添加到此文档的内容对象的参数列表。
16 public XDocument(params object[] content);
17 //
18 // 摘要:
19 // 从现有的 System.Xml.Linq.XDocument 对象初始化 System.Xml.Linq.XDocument 类的新实例。
20 //
21 // 参数:
22 // other:
23 // 要复制的 System.Xml.Linq.XDocument 对象。
24 public XDocument(XDocument other);
25 //
26 // 摘要:
27 // 用指定的 System.Xml.Linq.XDeclaration 和内容初始化 System.Xml.Linq.XDocument 类的新实例。
28 //
29 // 参数:
30 // declaration:
31 // 文档的 System.Xml.Linq.XDeclaration。
32 //
33 // content:
34 // 文档的内容。
35 [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
36 public XDocument(XDeclaration declaration, params object[] content);
37
38 // 摘要:
39 // 获取或设置此文档的 XML 声明。
40 //
41 // 返回结果:
42 // 一个 System.Xml.Linq.XDeclaration,其中包含此文档的 XML 声明。
43 public XDeclaration Declaration { get; set; }
44 //
45 // 摘要:
46 // 获取此文档的文档类型定义 (DTD)。
47 //
48 // 返回结果:
49 // 一个包含此文档 DTD 的 System.Xml.Linq.XDocumentType。
50 public XDocumentType DocumentType { get; }
51 //
52 // 摘要:
53 // 获取此节点的节点类型。
54 //
55 // 返回结果:
56 // 节点类型。对于 System.Xml.Linq.XDocument 对象,此值为 System.Xml.XmlNodeType.Document。
57 public override XmlNodeType NodeType { get; }
58 //
59 // 摘要:
60 // 获取此文档的 XML 树的根元素。
61 //
62 // 返回结果:
63 // XML 树的根 System.Xml.Linq.XElement。
64 public XElement Root { get; }
65
66 // 摘要:
67 // 使用指定的流创建一个新的 System.Xml.Linq.XDocument 实例。
68 //
69 // 参数:
70 // stream:
71 // 包含 XML 数据的流。
72 //
73 // 返回结果:
74 // 一个可读取流中所包含数据的 System.Xml.Linq.XDocument 对象。
75 [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
76 public static XDocument Load(Stream stream);
77 //
78 // 摘要:
79 // 从文件创建新 System.Xml.Linq.XDocument。
80 //
81 // 参数:
82 // uri:
83 // 一个 URI 字符串,它引用要加载到新 System.Xml.Linq.XDocument 中的文件。
84 //
85 // 返回结果:
86 // 一个包含所指定文件的内容的 System.Xml.Linq.XDocument。
87 [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
88 public static XDocument Load(string uri);
89 //
90 // 摘要:
91 // 从 System.IO.TextReader 创建新的 System.Xml.Linq.XDocument。
92 //
93 // 参数:
94 // textReader:
95 // 一个 System.IO.TextReader,其中包含 System.Xml.Linq.XDocument 的内容。
96 //
97 // 返回结果:
98 // 一个 System.Xml.Linq.XDocument,其中包含指定 System.IO.TextReader 的内容。
99 [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
100 public static XDocument Load(TextReader textReader);
101 //
102 // 摘要:
103 // 从 System.Xml.XmlReader 创建新 System.Xml.Linq.XDocument。
104 //
105 // 参数:
106 // reader:
107 // 一个 System.Xml.XmlReader,其中包含 System.Xml.Linq.XDocument 的内容。
108 //
109 // 返回结果:
110 // 一个 System.Xml.Linq.XDocument,其中包含指定的 System.Xml.XmlReader 的内容。
111 [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
112 public static XDocument Load(XmlReader reader);
113 //
114 // 摘要:
115 // 使用指定流创建新的 System.Xml.Linq.XDocument 实例,也可以选择保留空白,设置基 URI 和保留行信息。
116 //
117 // 参数:
118 // stream:
119 // 包含 XML 数据的流。
120 //
121 // options:
122 // 一个 System.Xml.Linq.LoadOptions,指定是否加载基 URI 和行信息。
123 //
124 // 返回结果:
125 // 一个可读取流中所包含数据的 System.Xml.Linq.XDocument 对象。
126 public static XDocument Load(Stream stream, LoadOptions options);
127 //
128 // 摘要:
129 // 从文件创建新 System.Xml.Linq.XDocument,还可以选择保留空白和行信息以及设置基 URI。
130 //
131 // 参数:
132 // uri:
133 // 一个 URI 字符串,它引用要加载到新 System.Xml.Linq.XDocument 中的文件。
134 //
135 // options:
136 // 一个 System.Xml.Linq.LoadOptions,指定空白行为以及是否加载基 URI 和行信息。
137 //
138 // 返回结果:
139 // 一个包含所指定文件的内容的 System.Xml.Linq.XDocument。
140 public static XDocument Load(string uri, LoadOptions options);
141 //
142 // 摘要:
143 // 从 System.IO.TextReader 创建新 System.Xml.Linq.XDocument,还可以选择保留空白和行信息以及设置基 URI。
144 //
145 // 参数:
146 // textReader:
147 // 一个 System.IO.TextReader,其中包含 System.Xml.Linq.XDocument 的内容。
148 //
149 // options:
150 // 一个 System.Xml.Linq.LoadOptions,指定空白行为以及是否加载基 URI 和行信息。
151 //
152 // 返回结果:
153 // 一个 System.Xml.Linq.XDocument,其中包含从指定的 System.IO.TextReader 读取的 XML。
154 public static XDocument Load(TextReader textReader, LoadOptions options);
155 //
156 // 摘要:
157 // 从 System.Xml.XmlReader 加载 System.Xml.Linq.XElement,还可以选择设置基 URI 和保留行信息。
158 //
159 // 参数:
160 // reader:
161 // 一个从其读取 System.Xml.Linq.XDocument 内容的 System.Xml.XmlReader。
162 //
163 // options:
164 // 一个 System.Xml.Linq.LoadOptions,指定是否加载基 URI 和行信息。
165 //
166 // 返回结果:
167 // 一个 System.Xml.Linq.XDocument,其中包含从指定的 System.Xml.XmlReader 读取的 XML。
168 public static XDocument Load(XmlReader reader, LoadOptions options);
169 //
170 // 摘要:
171 // 从字符串创建新 System.Xml.Linq.XDocument。
172 //
173 // 参数:
174 // text:
175 // 包含 XML 的字符串。
176 //
177 // 返回结果:
178 // 一个使用包含 XML 的字符串填充的 System.Xml.Linq.XDocument。
179 [TargetedPatchingOptOut("Performance critical to inline this type of method across NGen image boundaries")]
180 public static XDocument Parse(string text);
181 //
182 // 摘要:
183 // 从字符串创建新 System.Xml.Linq.XDocument,还可以选择保留空白和行信息以及设置基 URI。
184 //
185 // 参数:
186 // text:
187 // 包含 XML 的字符串。
188 //
189 // options:
190 // 一个 System.Xml.Linq.LoadOptions,指定空白行为以及是否加载基 URI 和行信息。
191 //
192 // 返回结果:
193 // 一个使用包含 XML 的字符串填充的 System.Xml.Linq.XDocument。
194 public static XDocument Parse(string text, LoadOptions options);
195 //
196 // 摘要:
197 // 将此 System.Xml.Linq.XDocument 输出到指定的 System.IO.Stream。
198 //
199 // 参数:
200 // stream:
201 // 要向其中输出此 System.Xml.Linq.XDocument 的流。
202 public void Save(Stream stream);
203 //
204 // 摘要:
205 // 将此 System.Xml.Linq.XDocument 序列化为文件。
206 //
207 // 参数:
208 // fileName:
209 // 一个包含文件名称的字符串。
210 public void Save(string fileName);
211 //
212 // 摘要:
213 // 将此 System.Xml.Linq.XDocument 序列化为 System.IO.TextWriter。
214 //
215 // 参数:
216 // textWriter:
217 // 将向其中写入 System.Xml.Linq.XDocument 的 System.IO.TextWriter。
218 public void Save(TextWriter textWriter);
219 //
220 // 摘要:
221 // 将此 System.Xml.Linq.XDocument 序列化为 System.Xml.XmlWriter。
222 //
223 // 参数:
224 // writer:
225 // 将向其中写入 System.Xml.Linq.XDocument 的 System.Xml.XmlWriter。
226 public void Save(XmlWriter writer);
227 //
228 // 摘要:
229 // 将此 System.Xml.Linq.XDocument 输出到指定的 System.IO.Stream,也可选择指定格式设置行为。
230 //
231 // 参数:
232 // stream:
233 // 要向其中输出此 System.Xml.Linq.XDocument 的流。
234 //
235 // options:
236 // 一个指定格式设置行为的 System.Xml.Linq.SaveOptions。
237 public void Save(Stream stream, SaveOptions options);
238 //
239 // 摘要:
240 // 将此 System.Xml.Linq.XDocument 序列化为文件,还可以选择禁用格式设置。
241 //
242 // 参数:
243 // fileName:
244 // 一个包含文件名称的字符串。
245 //
246 // options:
247 // 一个指定格式设置行为的 System.Xml.Linq.SaveOptions。
248 public void Save(string fileName, SaveOptions options);
249 //
250 // 摘要:
251 // 将此 System.Xml.Linq.XDocument 序列化为 System.IO.TextWriter,还可以选择禁用格式设置。
252 //
253 // 参数:
254 // textWriter:
255 // 要将 XML 输出到的 System.IO.TextWriter。
256 //
257 // options:
258 // 一个指定格式设置行为的 System.Xml.Linq.SaveOptions。
259 public void Save(TextWriter textWriter, SaveOptions options);
260 //
261 // 摘要:
262 // 将此文档写入 System.Xml.XmlWriter。
263 //
264 // 参数:
265 // writer:
266 // 此方法将写入的 System.Xml.XmlWriter。
267 public override void WriteTo(XmlWriter writer);
268 }
我们可以看到其实XDocument是有公共的构造函数的,而且还有不同类型参数的重载。而且可以看到它有一个Save方法,可以将XDocument类型存储为流和文件。于此同时,我们看到了一个公共的静态Load方法,用来载入一个XML。所以如果你想要读取一个XML,对XML的节点进行操作的话,想要用new的话是有些困难的。
string path = @"D:\Audit.sql";
if (File.Exists(path))
{
    XDocument xDoc = XDocument.Load(File.OpenText(path));
    xDoc.Elements().FirstOrDefault(x => string.Equals(x.Name.LocalName, "name", StringComparison.OrdinalIgnoreCase));
}

 

DataRow

DataRow Class: Represents a row of data in a DataTable.

继承关系:

Inheritance Hierarchy

System.Object

  -System.Data.DataRow

DataRow是为DataTable的行。可以我们发现,它貌似没有公用的构造函数,这可如何是好呢?

我们来看一下DataRow的作用是什么。它主要是作为DataTable的行,作为DataTable的Rows属性的Add方法的参数,为DataTable添加一条行数据。那我们是不是可以从DataTable类入手呢?我们查看一下DataTable的metaData。我们要去找什么样的呢?找返回类型是DataRow的方法。发现有:

public DataRow LoadDataRow(object[] values, bool fAcceptChanges);
public DataRow LoadDataRow(object[] values, LoadOption loadOption);
public DataRow NewRow();
protected virtual DataRow NewRowFromBuilder(DataRowBuilder builder);

公用方法有三个,有LoadDataRow,这个方法用来查找和更新特定行。如果找不到任何匹配行,则使用给定值创建新行。NewRow方法,用来创建与该表具有相同架构的新 System.Data.DataRow。这样我们就能初始化一个DataRow对象了。

DataTable dt = new DataTable();
DataColumn column = new DataColumn();
column.ColumnName = "Column1";
dt.Columns.Add(column);
DataRow row = dt.NewRow();
row["Column1"] = 12;
dt.Rows.Add(row);
Console.WriteLine(dt.Rows.Count);

 

Summary

  第一种,没有构造函数。我们可以尝试着去用它的子类去初始化它。

  第二种,虽然有构造函数,但是构造函数并不容易达到某种目的时,我们可以尝试用某些方法去初始化对象,从而达到某种目的,如载入XML文件。

  第三种,没有构造函数,且没有任何子类,我们可以通过这个类的作用出发,寻找突破口。(想法,其实如果某些类没有共有的构造函数,但是有受保护的构造函数,且这个类可以被继承,如果有需要的话,我们可以继承这个类,从而调用父类受保护的构造函数初始化它。)

  所以,我们在初始化一个对象时,但这个对象不是那么容易被初始化时,我们可以通过以上几种方法,尝试着初始化这个对象。

自用的。谢绝转载。有错的话,欢迎大家踊跃指出,我会加以修改的。  2012-03-31,22:19:42