📄 chap17.lst
字号:
listing 1
// Demonstrate is.
using System;
class A {}
class B : A {}
class UseIs {
public static void Main() {
A a = new A();
B b = new B();
if(a is A) Console.WriteLine("a is an A");
if(b is A)
Console.WriteLine("b is an A because it is derived from A");
if(a is B)
Console.WriteLine("This won't display -- a not derived from B");
if(b is B) Console.WriteLine("B is a B");
if(a is object) Console.WriteLine("a is an object");
}
}
listing 2
// Use is to avoid an invalid cast.
using System;
class A {}
class B : A {}
class CheckCast {
public static void Main() {
A a = new A();
B b = new B();
// Check to see if a can be cast to B.
if(a is B) // if so, do the cast
b = (B) a;
else // if not, skip the cast
b = null;
if(b==null)
Console.WriteLine("Cast b = (B) a is NOT allowed.");
else
Console.WriteLine("Cast b = (B) a is allowed");
}
}
listing 3
// Demonstrate as.
using System;
class A {}
class B : A {}
class CheckCast {
public static void Main() {
A a = new A();
B b = new B();
b = a as B; // cast, if possible
if(b==null)
Console.WriteLine("Cast b = (B) a is NOT allowed.");
else
Console.WriteLine("Cast b = (B) a is allowed");
}
}
listing 4
// Demonstrate typeof.
using System;
using System.IO;
class UseTypeof {
public static void Main() {
Type t = typeof(StreamReader);
Console.WriteLine(t.FullName);
if(t.IsClass) Console.WriteLine("Is a class.");
if(t.IsAbstract) Console.WriteLine("Is abstract.");
else Console.WriteLine("Is concrete.");
}
}
listing 5
// Analyze methods using reflection.
using System;
using System.Reflection;
class MyClass {
int x;
int y;
public MyClass(int i, int j) {
x = i;
y = j;
}
public int sum() {
return x+y;
}
public bool isBetween(int i) {
if(x < i && i < y) return true;
else return false;
}
public void set(int a, int b) {
x = a;
y = b;
}
public void set(double a, double b) {
x = (int) a;
y = (int) b;
}
public void show() {
Console.WriteLine(" x: {0}, y: {1}", x, y);
}
}
class ReflectDemo {
public static void Main() {
Type t = typeof(MyClass); // get a Type object representing MyClass
Console.WriteLine("Analyzing methods in " + t.Name);
Console.WriteLine();
Console.WriteLine("Methods supported: ");
MethodInfo[] mi = t.GetMethods();
// Display methods supported by MyClass.
foreach(MethodInfo m in mi) {
// Display return type and name.
Console.Write(" " + m.ReturnType.Name +
" " + m.Name + "(");
// Display parameters.
ParameterInfo[] pi = m.GetParameters();
for(int i=0; i < pi.Length; i++) {
Console.Write(pi[i].ParameterType.Name +
" " + pi[i].Name);
if(i+1 < pi.Length) Console.Write(", ");
}
Console.WriteLine(")");
Console.WriteLine();
}
}
}
listing 6
// Invoke methods using reflection.
using System;
using System.Reflection;
class MyClass {
int x;
int y;
public MyClass(int i, int j) {
x = i;
y = j;
}
public int sum() {
return x+y;
}
public bool isBetween(int i) {
if((x < i) && (i < y)) return true;
else return false;
}
public void set(int a, int b) {
Console.Write("Inside set(int, int). ");
x = a;
y = b;
show();
}
// Overload set.
public void set(double a, double b) {
Console.Write("Inside set(double, double). ");
x = (int) a;
y = (int) b;
show();
}
public void show() {
Console.WriteLine("Values are x: {0}, y: {1}", x, y);
}
}
class InvokeMethDemo {
public static void Main() {
Type t = typeof(MyClass);
MyClass reflectOb = new MyClass(10, 20);
int val;
Console.WriteLine("Invoking methods in " + t.Name);
Console.WriteLine();
MethodInfo[] mi = t.GetMethods();
// Invoke each method.
foreach(MethodInfo m in mi) {
// Get the parameters.
ParameterInfo[] pi = m.GetParameters();
if(m.Name.CompareTo("set")==0 &&
pi[0].ParameterType == typeof(int)) {
object[] args = new object[2];
args[0] = 9;
args[1] = 18;
m.Invoke(reflectOb, args);
}
else if(m.Name.CompareTo("set")==0 &&
pi[0].ParameterType == typeof(double)) {
object[] args = new object[2];
args[0] = 1.12;
args[1] = 23.4;
m.Invoke(reflectOb, args);
}
else if(m.Name.CompareTo("sum")==0) {
val = (int) m.Invoke(reflectOb, null);
Console.WriteLine("sum is " + val);
}
else if(m.Name.CompareTo("isBetween")==0) {
object[] args = new object[1];
args[0] = 14;
if((bool) m.Invoke(reflectOb, args))
Console.WriteLine("14 is between x and y");
}
else if(m.Name.CompareTo("show")==0) {
m.Invoke(reflectOb, null);
}
}
}
}
listing 7
// Create an object using reflection.
using System;
using System.Reflection;
class MyClass {
int x;
int y;
public MyClass(int i) {
Console.WriteLine("Constructing MyClass(int, int). ");
x = y = i;
}
public MyClass(int i, int j) {
Console.WriteLine("Constructing MyClass(int, int). ");
x = i;
y = j;
show();
}
public int sum() {
return x+y;
}
public bool isBetween(int i) {
if((x < i) && (i < y)) return true;
else return false;
}
public void set(int a, int b) {
Console.Write("Inside set(int, int). ");
x = a;
y = b;
show();
}
// Overload set.
public void set(double a, double b) {
Console.Write("Inside set(double, double). ");
x = (int) a;
y = (int) b;
show();
}
public void show() {
Console.WriteLine("Values are x: {0}, y: {1}", x, y);
}
}
class InvokeConsDemo {
public static void Main() {
Type t = typeof(MyClass);
int val;
// Get constructor info.
ConstructorInfo[] ci = t.GetConstructors();
Console.WriteLine("Available constructors: ");
foreach(ConstructorInfo c in ci) {
// Display return type and name.
Console.Write(" " + t.Name + "(");
// Display parameters.
ParameterInfo[] pi = c.GetParameters();
for(int i=0; i < pi.Length; i++) {
Console.Write(pi[i].ParameterType.Name +
" " + pi[i].Name);
if(i+1 < pi.Length) Console.Write(", ");
}
Console.WriteLine(")");
}
Console.WriteLine();
// Find matching constructor.
int x;
for(x=0; x < ci.Length; x++) {
ParameterInfo[] pi = ci[x].GetParameters();
if(pi.Length == 2) break;
}
if(x == ci.Length) {
Console.WriteLine("No matching constructor found.");
return;
}
else
Console.WriteLine("Two-parameter constructor found.\n");
// Construct the object.
object[] consargs = new object[2];
consargs[0] = 10;
consargs[1] = 20;
object reflectOb = ci[x].Invoke(consargs);
Console.WriteLine("\nInvoking methods on reflectOb.");
Console.WriteLine();
MethodInfo[] mi = t.GetMethods();
// Invoke each method.
foreach(MethodInfo m in mi) {
// Get the parameters.
ParameterInfo[] pi = m.GetParameters();
if(m.Name.CompareTo("set")==0 &&
pi[0].ParameterType == typeof(int)) {
// This is set(int, int).
object[] args = new object[2];
args[0] = 9;
args[1] = 18;
m.Invoke(reflectOb, args);
}
else if(m.Name.CompareTo("set")==0 &&
pi[0].ParameterType == typeof(double)) {
// This is set(double, double).
object[] args = new object[2];
args[0] = 1.12;
args[1] = 23.4;
m.Invoke(reflectOb, args);
}
else if(m.Name.CompareTo("sum")==0) {
val = (int) m.Invoke(reflectOb, null);
Console.WriteLine("sum is " + val);
}
else if(m.Name.CompareTo("isBetween")==0) {
object[] args = new object[1];
args[0] = 14;
if((bool) m.Invoke(reflectOb, args))
Console.WriteLine("14 is between x and y");
}
else if(m.Name.CompareTo("show")==0) {
m.Invoke(reflectOb, null);
}
}
}
}
listing 8
// A file that contains three classes. Call this file MyClasses.cs.
using System;
class MyClass {
int x;
int y;
public MyClass(int i) {
Console.WriteLine("Constructing MyClass(int). ");
x = y = i;
show();
}
public MyClass(int i, int j) {
Console.WriteLine("Constructing MyClass(int, int). ");
x = i;
y = j;
show();
}
public int sum() {
return x+y;
}
public bool isBetween(int i) {
if((x < i) && (i < y)) return true;
else return false;
}
public void set(int a, int b) {
Console.Write("Inside set(int, int). ");
x = a;
y = b;
show();
}
// Overload set.
public void set(double a, double b) {
Console.Write("Inside set(double, double). ");
x = (int) a;
y = (int) b;
show();
}
public void show() {
Console.WriteLine("Values are x: {0}, y: {1}", x, y);
}
}
class AnotherClass {
string remark;
public AnotherClass(string str) {
remark = str;
}
public void show() {
Console.WriteLine(remark);
}
}
class Demo {
public static void Main() {
Console.WriteLine("This is a placeholder.");
}
}
listing 9
/* Locate an assembly, determine types, and create
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -