Essential .net是好书,但是代码未给出来,这两天上www.gotdotnet.com/team/dbox也上不去,
故只能自己编写,测试一下了,也发现很多有趣的事情。
1。 按照道理来讲,静态或者构造函数生成时候顺序由声明顺序决定。但是在嵌套构造函数的时候第一个
声明的反而最慢,至少在我的机器上是这样
public sealed class customer
{
//这种方法最慢t1
public customer():this(System.DateTime.Now.Ticks)
{
//
// TODO: 在此处添加构造函数逻辑
//
}
public customer(long init)
{
t1 = init;
}
//按照声明顺序
internal static long t1;
internal static long t2 = System.DateTime.Now.Ticks;
internal static long t3 = System.DateTime.Now.Ticks;
}
2. 就算用了System.Reflection.BindingFlags.NonPublic/的标示仍然不能读出私有的数据!
public static void dumpMembers(System.Type type)
{
System.Reflection.BindingFlags f = System.Reflection.BindingFlags.Static
| System.Reflection.BindingFlags.Instance//在field中
| System.Reflection.BindingFlags.Public
| System.Reflection.BindingFlags.NonPublic//无私有的数据!
| System.Reflection.BindingFlags.FlattenHierarchy;//继承
System.Reflection.MemberInfo[] members = type.GetMembers();
for(int i = 0;i < members.Length; i++)
{
System.Console.WriteLine("{0} {1}",members[i].MemberType,members[i].Name);
}
}
3.书103页代码有问题
[
System.Runtime.CompilerServices.IndexerName("Quantities")
]
public decimal this[string partID]
{
get
{
switch(partID)
{
case "cWidgets":
return cWidgets;
case "cGizmos":
return cGizmos;
case "cDooDads":
return cDooDads;
default:
//InvalidArgumentException 居然在Microsoft.SqlServer.Management.Common这个名字空间了
throw new System.InvalidProgramException(partID);
}
}
set
{
switch(partID)
{
case "cWidgets":
cWidgets = value;
//不用break?
break;//自己添加
case "cGizmos":
cGizmos = value;
break;
case "cDooDads":
cDooDads = value;
break;
default:
throw new System.InvalidProgramException(partID);
}
}
}
internal decimal cWidgets;
internal decimal cGizmos;
internal decimal cDooDads;
//4自定义类型复制只能clone
public sealed class marriage : System.ICloneable
{
internal Person g;
internal Person b;
//私有化,使得不是clone的时候外部用不了,故而也指引不到自己上头来
private marriage(Person g,Person b)
{
//这个this实际上已经是new的新的实例的引用了
this.g = (Person)(g .Clone());
this.b = (Person)(b .Clone());
}
public System.Object Clone()
{
//return this.MemberwiseClone();//CLR帮忙,浅拷贝,不管内容是否引用
//深拷贝,必须嵌套为ICloneable接口,以做最大限度保证
return new marriage(this.g,this.b);
}
}
public sealed class Person: System.ICloneable
{
public bool sex;
public System.Object Clone()
{
return this.MemberwiseClone();//CLR帮忙,浅拷贝,不管内容是否引用
}
}
5 //为了防止被垃圾站回收,经常要用一个静态的hashtable(省地方)来保存引用
public class FancyObjects
{
private string name;
private FancyObjects(string name)
{
this.name = name;
}
static System.Collections.IDictionary table = new System.Collections.Hashtable();
public static FancyObject Get(string name)
{
//这样的话 FanceObject就一个都不会被回收了
lock (typeof(FancyObjects))
{
FancyObject r = (FancyObject)table[name];
if( r== null)
{
r = new FancyObject(name);
table[name] = r;
}
return r;
}
}
//而实际上很多时候还是希望被回收的,所以用到了System.WeakReference,以使得目标unreachable
public static FancyObject GetEx(string name)
{
//这样的话 FanceObject就一个都不会被回收了
lock (typeof(FancyObjects))
{
System.WeakReference weak = (System.WeakReference)table[name];
FancyObject r = null;
if(weak != null)
{
r = (FancyObject)weak.Target;
}
if( r== null)
{
r = new FancyObject(name);
table[name] = new System.WeakReference(r);
}
return r;
}
}
static System.Object o1;
public static void collect()
{
o1 = new object();
System.Object o2 = new object();
System.Object o3 = new object();
System.GC.Collect(1);
o1 = null;
o3.ToString();
System.GC.KeepAlive(o1);
System.GC.Collect();
}
public static void work()
{
using(FancyObject o2 = new FancyObject(), o3 = new FancyObject())//必须是继承自System.IDisposable
//编译器在局部完成后自动插入Dispose()代码
{
}
}
}
public class FancyObject : System.IDisposable
{
string name;
public FancyObject(string name)
{
this.name = name;
}
public FancyObject()
{
this.name = "";
}
//此finalizer与C++析构函数是本质上不同的,这个函数被GC异步的调用
~FancyObject()
{
cleanUp();
}
//dispose方法才能让客户端程序员显示的清除资源
#region IDisposable 成员
public void Dispose()
{
// TODO: 添加 FancyObject.Dispose 实现
System.GC.SuppressFinalize(this);//有可能重复释放资源,因为干的活跟~FancyObject是一样的,所以设置为优先
cleanUp();
}
#endregion
public void cleanUp()
{}
}
嗷嗷混乱的代码
public interface ICommon
{
void DoIt();
}
public class BaseX : ICommon
{
void ICommon.DoIt()
{
System.Console.WriteLine("ICommon.DoIt in Base");
}
public virtual void DoIt()
{
System.Console.WriteLine("Base DoIt virtual");
}
}
public class DerivedX: Base,ICommon
{
void ICommon.DoIt()
{
System.Console.WriteLine("ICommon.DoIt in Derived");
}
public new virtual void DoIt()
{
System.Console.WriteLine("Derived DoIt virtual");
}
}
public class ReallyDerivedX: DerivedX
{
public override void DoIt()
{
System.Console.WriteLine("ReallyDerived DoIt virtual");
}
}
未完待续。。。。。。