Essential.net 代码调试

本文探讨了C#编程中的几个关键问题,包括构造函数执行顺序的特殊情况、使用反射访问私有成员的限制、索引器实现错误及修复、深拷贝与浅拷贝的区别、对象引用管理和垃圾回收机制等。

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");
  }
 }

未完待续。。。。。。

【完美复现】面向配电网韧性提升的移动储能预布局与动态调度策略【IEEE33节点】(Matlab代码实现)内容概要:本文介绍了基于IEEE33节点的配电网韧性提升方法,重点研究了移动储能系统的预布局与动态调度策略。通过Matlab代码实现,提出了一种结合预配置和动态调度的两阶段优化模型,旨在应对电网故障或极端事件时快速恢复供电能力。文中采用了多种智能优化算法(如PSO、MPSO、TACPSO、SOA、GA等)进行对比分析,验证所提策略的有效性和优越性。研究不仅关注移动储能单元的初始部署位置,还深入探讨其在故障发生后的动态路径规划与电力支援过程,从而全面提升配电网的韧性水平。; 适合人群:具备电力系统基础知识和Matlab编程能力的研究生、科研人员及从事智能电网、能源系统优化等相关领域的工程技术人员。; 使用场景及目标:①用于科研复现,特别是IEEE顶刊或SCI一区论文中关于配电网韧性、应急电源调度的研究;②支撑电力系统在灾害或故障条件下的恢复力优化设计,提升实际电网应对突发事件的能力;③为移动储能系统在智能配电网中的应用提供理论依据和技术支持。; 阅读建议:建议读者结合提供的Matlab代码逐模块分析,重点关注目标函数建模、约束条件设置以及智能算法的实现细节。同时推荐参考文中提及的MPS预配置与动态调度上下两部分,系统掌握完整的技术路线,并可通过替换不同算法或测试系统进一步拓展研究。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值