30.写了如下代码去实现 CompanyClass.MyMethod 方法:
public class CompanyClass {
public int MyMethod(int arg) {
return arg;
}}
你需要在你的程序集中使用一个和CompanyClass不相关的类动态的去调用
CompanyClass.MyMethod方法。你应该使用下面哪段代码?
A. CompanyClass myClass = new CompanyClass();
Type t = typeof(CompanyClass);
MethodInfo m = t.GetMethod("MyMethod");
int i = (int)m.Invoke(this, new object[] { 1 });
B. CompanyClass myClass = new CompanyClass();
Type t = typeof(CompanyClass);
MethodInfo m = t.GetMethod("MyMethod");
int i = (int) m.Invoke(myClass, new object[] { 1 });
C. CompanyClass myClass = new CompanyClass();
Type t = typeof(CompanyClass);
MethodInfo m = t.GetMethod("CompanyClass.MyMethod");
int i = (int)m.Invoke(myClass, new object[] { 1 });
D. Type t = Type.GetType("CompanyClass");
MethodInfo m = t.GetMethod("MyMethod");
int i = (int)m.Invoke(this, new object[] { 1 });
答案: B
31.你创建了一个如下的、包含类层次定义的类库:
(行号只作为引用标识,没有实际意义)
01 public class Group {
02 public Employee[] Employees;
03 }
04 public class Employee {
05 public string Name;
06 }
07 public class Manager : Employee {
08 public int Level;
09 }
32.你创建了一个Group类的实例并且为实例的字段进行了赋值。当你使用XmlSerializer类的
Serialize方法试图去序列化实例的时候,你收到了InvalidOperationException异常和错误消息:
"在产生XML文档时有一个错误。"。为此,你需要修改上述代码,使Group实例能够使用
XmlSerializer成功的序列化。同时你也需要保证,输出的XML包含所有类层次中的public属性。
你应该如何修改?
A. 在行1和行2之间插入:
[XmlArrayItem(Type = typeof(Employee))]
[XmlArrayItem(Type = typeof(Manager))]
B. 在行1和行2之间插入:
[XmlElement(Type = typeof(Employees))]
C. 在行1和行2之间插入:
[XmlArray(ElementName="Employees")]
D. 在行3和行4之间插入:
[XmlElement(Type = typeof(Employee))]
并且在行6和行7之间插入:
[XmlElement(Type = typeof(Manager))]
答案: A