日有所思

微创社(MCC) 2009-08-25 04:49:25
加精
权当记事板了,满三贴后,请朋友们帮顶一下.
[@]2009/08/28
[%]关于集合

using System.Collections;
using System.Runtime.CompilerServices;

namespace System.Collections.Generic
{
// 摘要:
// 公开枚举数,该枚举数支持在指定类型的集合上进行简单迭代。
//
// 类型参数:
// T:
// 要枚举的对象的类型。
public interface IEnumerable<T> : IEnumerable
{
// 摘要:
// 返回一个循环访问集合的枚举数。
//
// 返回结果:
// 可用于循环访问集合的 System.Collections.Generic.IEnumerator<T>。
IEnumerator<T> GetEnumerator();
}
}


using System.Runtime.InteropServices;

namespace System.Collections
{
// 摘要:
// 公开枚举数,该枚举数支持在非泛型集合上进行简单迭代。
[ComVisible(true)]
[Guid("496B0ABE-CDEE-11d3-88E8-00902754C43A")]
public interface IEnumerable
{
// 摘要:
// 返回一个循环访问集合的枚举数。
//
// 返回结果:
// 可用于循环访问集合的 System.Collections.IEnumerator 对象。
[DispId(-4)]
IEnumerator GetEnumerator();
}
}


using System;
using System.Collections;

namespace System.Collections.Generic
{
// 摘要:
// 支持在泛型集合上进行简单迭代。
//
// 类型参数:
// T:
// 要枚举的对象的类型。
public interface IEnumerator<T> : IDisposable, IEnumerator
{
// 摘要:
// 获取集合中位于枚举数当前位置的元素。
//
// 返回结果:
// 集合中位于枚举数当前位置的元素。
T Current { get; }
}
}


using System;
using System.Runtime.InteropServices;

namespace System.Collections
{
// 摘要:
// 支持对非泛型集合的简单迭代。
[ComVisible(true)]
[Guid("496B0ABF-CDEE-11d3-88E8-00902754C43A")]
public interface IEnumerator
{
// 摘要:
// 获取集合中的当前元素。
//
// 返回结果:
// 集合中的当前元素。
//
// 异常:
// System.InvalidOperationException:
// 枚举数定位在该集合的第一个元素之前或最后一个元素之后。- 或 - 在创建了枚举数后集合被修改了。
object Current { get; }

// 摘要:
// 将枚举数推进到集合的下一个元素。
//
// 返回结果:
// 如果枚举数成功地推进到下一个元素,则为 true;如果枚举数越过集合的结尾,则为 false。
//
// 异常:
// System.InvalidOperationException:
// 在创建了枚举数后集合被修改了。
bool MoveNext();
//
// 摘要:
// 将枚举数设置为其初始位置,该位置位于集合中第一个元素之前。
//
// 异常:
// System.InvalidOperationException:
// 在创建了枚举数后集合被修改了。
void Reset();
}
}


using System.Runtime.InteropServices;

namespace System
{
// 摘要:
// 定义一种释放分配的资源的方法。
[ComVisible(true)]
public interface IDisposable
{
// 摘要:
// 执行与释放或重置非托管资源相关的应用程序定义的任务。
void Dispose();
}
}

...全文
21208 363 打赏 收藏 转发到动态 举报
写回复
用AI写文章
363 条回复
切换为时间正序
请发表友善的回复…
发表回复
huang_137151 2011-09-21
  • 打赏
  • 举报
回复
有空再来看,先留印记
微创社(MCC) 2011-01-01
  • 打赏
  • 举报
回复
回头看看,一年前自学的东西。
现在己经完全看不明白了。

经过一个月的反省...
本人正在研究PPT中...

简洁的就是简单的,
实用的就是可用的。

创建中国第一PPT专业技术网站...
dreams1981 2009-11-26
  • 打赏
  • 举报
回复
学校了,MARK一个
微创社(MCC) 2009-09-19
  • 打赏
  • 举报
回复
关于:CodePaste.NET

http://codepaste.net/
http://www.west-wind.com:8080/svn/codepaste/trunk/

微创社(MCC) 2009-09-19
  • 打赏
  • 举报
回复
.net FrameWork下的四个,赖注入框架
[1]Unity,也就是微软企业库Unity Application Block
[2]Composite Application Library,主要在WPF和Silverlight中使用
[3]CLR插件模型,也就是MAF
[4]MEF(托管扩展框架)

第三方框架
Spring.NET

http://channel9.msdn.com/shows/10-4/10-4-Episode-26-Creating-Extensible-Applications-with-the-Managed-Extensibility-Framework/
微创社(MCC) 2009-09-18
  • 打赏
  • 举报
回复
转一个:修改一个.net程序(或者说打补丁)的几种方法:
[1]十六进制编辑软件(如WinHex、UltraEdit)等搜索MSIL的硬编码,然后替换;
[2]ildasm反编译后,ilasm再编译的round-tripping操作;
[3]运用Cecil编写的工具(如Reflector中的ReflexIL插件,或是Simple Assembly Explorer),在MSIL基础上直接修改;
[4]导出成高级语言项目,然后修改后在VS中编译(很少遇到这种情况)。
[5]利用Rebel.NET生成的rebel文件,在CFF中修改该文件,然后利用Rebel.NET来patch原文件。
aolang1987 2009-09-18
  • 打赏
  • 举报
回复
看不懂耶
Flyingdragon168 2009-09-18
  • 打赏
  • 举报
回复
sxmonsy 2009-09-18
  • 打赏
  • 举报
回复
UP
litchi123 2009-09-18
  • 打赏
  • 举报
回复
虽然有些没看懂,但学习了
whtsp 2009-09-17
  • 打赏
  • 举报
回复
微创社(MCC) 2009-09-17
  • 打赏
  • 举报
回复
CLR执行托管代码的第一行代码前
[1]系统域
系统域负责创建和初始化共享域和默认应用程序域。
它将系统库mscorlib.dll载入共享域,
并且维护进程范围内部使用的隐含或者显式字符串符号。

[2]共享域
所有不属于任何特定域的代码被加载到系统库SharedDomain.Mscorlib,
对于所有应用程序域的用户代码都是必需的。
它会被自动加载到共享域中。

[3]默认程序域
默认域是应用程序域(AppDomain)的一个实例,一般的应用程序代码在其中运行。

[4]默认程序域加载,四个堆/Heap
a)进程/Process堆:
b)JIT堆:存放编译后代码的地方
c)GC堆:存放小对象
d)LOH/Large Object堆:存放大对象


而GC和LOH是存放对象的地方。
对象和各自的方法是通过方法表(Method Tables)连接起来的。

[1]变量
保存在线程堆上

[2]引用对象
保存在堆上
对象都有额外的两个字段
a)同步索引块
b)类型对象指针

而GC和LOH是存放对象的地方。
对象和各自的方法是通过方法表(Method Tables)连接起来的。

//-------------- 内存布局 ----------
// written by 儒道佛 @2009.09
// relation to pc@mye.cn
//----------------------------------

namespace PCTools.Memory
{
using System;
using System.Runtime.InteropServices;

public interface IPointable
{
IntPtr Pointer { get; set; }
int Read(int ofs);
void Write(int ofs, int val);
}

public class PointerBase : IPointable
{
public virtual IntPtr Pointer { get; set; }
public virtual int PointerVal { get; set; }

public virtual int Read(int ofs) { return Marshal.ReadInt32(this.Pointer, ofs); }
public virtual void Write(int ofs, int val) { Marshal.WriteInt32(this.Pointer, ofs, val); }
}

public static class Tools
{
//静态方法
public static void Show(IntPtr ptr, int ofs, int val, string tips)
{
string sVal = string.Format("{0:x8}", (uint)val);
int tmp;
tmp = (val & 0x000000FF) >> 0;
char c1 = Convert.ToChar((tmp >= 32 ? (tmp <= 127 ? tmp : 46) : 46));
tmp = (val & 0x0000FF00) >> 8;
char c2 = Convert.ToChar((tmp >= 32 ? (tmp <= 127 ? tmp : 46) : 46));
tmp = (val & 0x00FF0000) >> 16;
char c3 = Convert.ToChar((tmp >= 32 ? (tmp <= 127 ? tmp : 46) : 46));
tmp = val >> 24;
char c4 = Convert.ToChar((tmp >= 32 ? (tmp <= 127 ? tmp : 46) : 46));

Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.Gray;
Console.Write("0x{0:X8} ", (uint)(ptr.ToInt32() + ofs));

Console.ForegroundColor = ConsoleColor.Black;
Console.Write("{0}{1} {2}{3} {4}{5} {6}{7} ", sVal[6], sVal[7], sVal[4], sVal[5], sVal[2], sVal[3], sVal[0], sVal[1]);

Console.ForegroundColor = ConsoleColor.Black;
Console.Write("{0}{1}{2}{3} ", c1, c2, c3, c4);

Console.ForegroundColor = ConsoleColor.Gray;
Console.WriteLine("{0,-30}", tips);
Console.ResetColor();
}

public static void Show(IntPtr ptr, int val, string tips)
{
Show(ptr, 0, val, tips);
}

public static void ShowArea(IntPtr ptr, int ofs, int len, string tips)
{
int curOfs = ofs;
int tmpVal;
string curTips;

Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.DarkGreen;
Console.WriteLine("{0,-61}","-------------- " + tips + " --------------");
Console.ResetColor();
for (int i = 0; i < len; i++)
{
curTips = string.Format("ofs:{0}", curOfs.ToString());
tmpVal = Marshal.ReadInt32(ptr, curOfs);
Show(ptr, curOfs, tmpVal, curTips);
curOfs += 4;
}
}

public static void ShowArea(IntPtr ptr, int len, string tips)
{
ShowArea(ptr, 0, len, tips);
}

//扩展方法
public static void Show(this IPointable ptr, int ofs, string tips)
{
int val = Marshal.ReadInt32(ptr.Pointer, ofs);
Show(ptr.Pointer, ofs, val, tips);
}

public static void ShowArea(this IPointable ptr, int ofs, int len, string tips)
{
ShowArea(ptr.Pointer, ofs, len, tips);
}
}

//------------ ObjectInstance --------------
// -04(04):SyncblkIndex
// +00(04):MethodTable
// +??(??):ComponentCount
//------------------------------------------
public class ObjectInstance : PointerBase, IPointable, IDisposable
{
private const int SyncblkIndex_Ofs = -4;
private const int MethodTablePointer_Ofs = 0;

//字段
private object _obj;
private GCHandle _gc;
private IntPtr _ptr_gc;
private IntPtr _ptr;

//属性
public override IntPtr Pointer
{
get { return _ptr; }
set { throw new Exception("non set"); }
}
public override int PointerVal
{
get { return Marshal.ReadInt32(Pointer); }
set { throw new Exception("non set"); }
}
public IntPtr GcPtr { get { return _ptr_gc; } }
public int GcVal { get { return Marshal.ReadInt32(GcPtr); } }
public IntPtr SyncblkIndexPtr { get { return new IntPtr(_ptr.ToInt32() + SyncblkIndex_Ofs); } }
public int SyncblkIndexVal { get { return Marshal.ReadInt32(SyncblkIndexPtr); } }
public IntPtr MethodTablePointerPtr { get { return new IntPtr(_ptr.ToInt32() + MethodTablePointer_Ofs); } }
public int MethodTablePointerVal { get { return Marshal.ReadInt32(MethodTablePointerPtr); } }

//构造
public ObjectInstance(object obj)
{
if (obj == null) throw new ArgumentNullException();

this._obj = obj;
this._gc = GCHandle.Alloc(obj);
this._ptr_gc = GCHandle.ToIntPtr(_gc);
this._ptr = Marshal.ReadIntPtr(_ptr_gc);
}

//诉构
~ObjectInstance() { (this as IDisposable).Dispose(); }
void IDisposable.Dispose() { if (_gc.IsAllocated)_gc.Free(); }
}

//------------ MethodTable --------------
// -12(12):GCInfo
// +00(04):Flags
// +04(04):Basic Instance Size
// +08(04):EEClass
// +12(04):Interface Vtable Map
// +16(02):NumInterfaces
// +18(02):CorElementType
// +20(04):Module
// +24(04):.cctor Slot
// +26(02):Default .ctor Slot
// +28(04):Interface Map
// +32(04):Delegate
// +36(04):Num Method Slots
// +40(04):ToString...
// +??(04):Equals...
// .......:...........
// +??(04):...........
// +??(04):static string str
// .......:...........
// +??(04):Flags|Impl Start Slot...
// +??(04):MyInterface1 TypeHandle...
// +?2(04):Flags|Impl Start Slot...
// +??(04):MyInterface2 TypeHandle...
// +??(04):...........
// +??(04):...........
//------------------------------------------
public class MethodTable : PointerBase, IPointable
{


}
}

namespace Run
{
using System;
using PCTools.Memory;

interface IA
{
void Test();
}

class A : IA
{
private int i;
public int I
{
get { return i; }
set { i = value; }
}
public A() { this.i = 100; }
public A(int i) { this.i = i; }
public void Test()
{
Console.WriteLine(I);
}

class MyTest
{
static void Main()
{
A a = new A();
ObjectInstance objectInstance = new ObjectInstance(a);

objectInstance.Show(0, "MethodTablePointer");
objectInstance.Show(-4, "SyncblkIndex");
Tools.Show(new IntPtr(0), a.GetHashCode(), "a's hashcode");
objectInstance.Show(-4, "SyncblkIndex");
Tools.ShowArea((IntPtr)objectInstance.MethodTablePointerVal, 20, "MethodTablePointer'0~20");

Console.ReadKey();
}
}
}
}
seamone 2009-09-17
  • 打赏
  • 举报
回复
支持一下。
微创社(MCC) 2009-09-17
  • 打赏
  • 举报
回复
泛型创建:

using System;
class MyClass
{
static Type GetType<T>()
{
return typeof(T);
}

static void Main(string[] args)
{
Console.WriteLine(GetType<int>());
}
}

//正确
class GenericFactory<T>
where T : new()
{
public T Create()
{
return new T();
}
}

//出错:“T”: 创建变量类型的实例时无法提供参数
class GenericFactory<T>
where T : new()
{
public T Create(object a, object b)
{
return new T(a, b);
}
}

//使用反射
class GenericFactory<T>
{
public T Create<T>(object a, object b)
{

return (T)typeof(T).GetConstructor(
new System.Type[] { typeof(object), typeof(object) }).Invoke(new object[] { a, b });
}
}

//使用接口
interface I
{
void Initialize(object a, object b);
}

class GenericFactory<T>
where T : I, new()
{
public T Create(object a, object b)
{
T t = new T();
t.Initialize(a, b);
return t;
}
}


Xzoth 2009-09-16
  • 打赏
  • 举报
回复
Mark
newsysway 2009-09-16
  • 打赏
  • 举报
回复
学习一下,顶!
微创社(MCC) 2009-09-15
  • 打赏
  • 举报
回复
object结构初探:



先从ObjectHeader入手,放个卫星先,
最近有点忙,慢慢加...
using System;
using System.Runtime.InteropServices;

class ObjectHeader : IDisposable
{
//字段-内部
private object _obj;
private GCHandle _gc;


//字段-属性
private IntPtr pGC;
private IntPtr pSyncblkIndex;//offset -4
private IntPtr pMethodTablePointer;//offset 0
private IntPtr pComponentCount;

//构造
public ObjectHeader(object obj)
{
if (obj == null) throw new ArgumentNullException();

this._obj = obj;
this._gc = GCHandle.Alloc(obj);

this.pGC = GCHandle.ToIntPtr(_gc);
this.pMethodTablePointer = Marshal.ReadIntPtr(pGC);
this.pSyncblkIndex = new IntPtr(Marshal.ReadInt32(pGC) - IntPtr.Size);
}

//拆构
~ObjectHeader() { (this as IDisposable).Dispose(); }
void IDisposable.Dispose() { if (_gc.IsAllocated)_gc.Free(); }

//指针
public IntPtr PtrOfGC { get { return pGC; } }
public IntPtr PtrOfSyncblkIndex { get { return pSyncblkIndex; } }
public IntPtr PtrOfMethodTablePointer { get { return pMethodTablePointer; } }

//指针值
public int ValOfGC { get { return Marshal.ReadInt32(pGC); } }
public int ValOfSyncblkIndex { get { return Marshal.ReadInt32(pSyncblkIndex); } }
public int ValOfMethodTablePointer { get { return Marshal.ReadInt32(pMethodTablePointer); } }
public int HashCode
{
get
{
int HashCodeMask = 0x03FFFFFF;
return ValOfSyncblkIndex & HashCodeMask;
}
}

//方法
public void PrintAll(string tips)
{
Console.WriteLine("========== " + tips + " ==========");
Console.WriteLine("{0,23}:{1:X8}", "PtrOfGC",(uint)PtrOfGC);
Console.WriteLine("{0,23}:{1:X8}", "PtrOfSyncblkIndex", (uint)PtrOfSyncblkIndex);
Console.WriteLine("{0,23}:{1:X8}", "PtrOfMethodTablePointer", (uint)PtrOfMethodTablePointer);
Console.WriteLine("{0,23}:{1:X8}", "ValOfGC", (uint)ValOfGC);
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("{0,23}:{1:X8}", "ValOfSyncblkIndex", (uint)ValOfSyncblkIndex);
Console.ResetColor();
Console.WriteLine("{0,23}:{1:X8}", "ValOfMethodTablePointer", (uint)ValOfMethodTablePointer);
Console.WriteLine("{0,23}:{1:X8}", "HashCode", (uint)HashCode);
}
}

public class MethodTable
{
public int i = 100;
}

class BBB
{
public int x = 100;
}

class MyClass
{
static void Main()
{
BBB b = new BBB();
ObjectHeader header = new ObjectHeader(b);

//原始
header.PrintAll("原始");
//hash后
b.GetHashCode();
header.PrintAll("hash");
//lock后
lock (b) { };
header.PrintAll("lock");

Console.ReadKey();
}
}
微创社(MCC) 2009-09-15
  • 打赏
  • 举报
回复
MethodTable Layout



CGInfo
Flags
Basic Instance Size
EEClass
Interface Vtable Map
Num Interfaces
CoreElementType
Module
.cctor Slot
Default .ctor Slot
Interface Map
Delegate
Mum Method Solts

微创社(MCC) 2009-09-15
  • 打赏
  • 举报
回复
Size: 12(0xc) bytes
MethodTable: 02e1d0f0
EEClass: 02e1d08c
Module: 02de2000

0x02E1D0F0 00 00 04 02 ....
0x02E1D0F4 0c 00 00 00 ....
0x02E1D0F8 22 04 0e 00 "...
0x02E1D0FC 04 00 00 00 ....
0x02E1D100 00 00 00 00 ....
0x02E1D104 00 20 de 02 . ..
0x02E1D108 54 47 de 02 TG..
0x02E1D10C 8c d0 e1 02 ....
0x02E1D110 00 00 00 00 ....
0x02E1D114 00 00 00 00 ....
0x02E1D118 78 12 09 03 x...
0x02E1D11C b0 b3 08 03 ....
0x02E1D120 d0 b3 08 03 ....
0x02E1D124 d0 24 08 03 .$..
0x02E1D128 b8 24 08 03 .$..
0x02E1D12C 7a 25 ef 79 z%.y
0x02E1D130 70 5b 09 03 p[..
0x02E1D134 94 5b 09 03 .[..
0x02E1D138 71 01 e8 79 q..y
0x02E1D13C 04 16 3e 03 ..>.
0x02E1D140 18 16 3e 03 ..>.
0x02E1D144 2c 16 3e 03 ,.>.
0x02E1D148 40 16 3e 03 @.>.
0x02E1D14C 54 16 3e 03 T.>.
0x02E1D150 74 f1 f6 02 t...
0x02E1D154 00 00 00 00 ....
0x02E1D158 00 20 de 02 . ..
0x02E1D15C 06 00 00 02 ....
0x02E1D160 b8 d1 e1 02 ....
0x02E1D164 ff ff 04 00 ....
0x02E1D168 12 00 00 00 ....
0x02E1D16C 00 00 00 00 ....
0x02E1D170 00 00 00 00 ....
0x02E1D174 00 00 00 00 ....
0x02E1D178 ff ff ff ff ....
0x02E1D17C ff ff ff ff ....
微创社(MCC) 2009-09-15
  • 打赏
  • 举报
回复
最小的内存图谱
class MyClass
{
static void Main()
{
object o = new object();
}
}


@stack
OS Thread Id: 0xb54 (2900)
0013f218 00be16f4 System.Object

00be16f4
Name: System.Object
MethodTable: 02e1d0f0
EEClass: 02e1d08c
Size: 12(0xc) bytes

@02e1d0f0
EEClass: 02e1d08c
Module: 02de2000
Name: System.Object
mdToken: 02000002
BaseSize: 0xc
ComponentSize: 0x0
Number of IFaces in IFaceMap: 0
Slots in VTable: 14
--------------------------------------
MethodDesc Table
Entry MethodDesc JIT Name
03091278 02e6b928 PreJIT ToString()
0308b3b0 02e6b930 PreJIT Equals(System.Object)
0308b3d0 02e6b948 PreJIT GetHashCode()
030824d0 02e6b950 PreJIT Finalize()
030824b8 02e6b920 PreJIT .ctor()
79ef257a 02e6b8f0 FCALL InternalEquals(System.Object, System.Object)
03095b70 02e6b938 PreJIT Equals(System.Object, System.Object)
03095b94 02e6b940 PreJIT ReferenceEquals(System.Object, System.Object)
79e80171 02e6b8f8 FCALL InternalGetHashCode(System.Object)
033e1604 02e6b900 FCALL GetType()
033e1618 02e6b908 FCALL MemberwiseClone()
033e162c 02e6b958 PreJIT FieldSetter(System.String, System.String, System.Object)
033e1640 02e6b960 PreJIT FieldGetter(System.String, System.String, System.Object ByRef)
033e1654 02e6b968 PreJIT GetFieldInfo(System.String, System.String)

@02e1d08c
Class Name: System.Object
mdToken: 02000002
Parent Class: 00000000
Module: 02de2000
Method Table: 02e1d0f0
Vtable Slots: 4
Total Method Slots: e
Class Attributes: 102001
NumInstanceFields: 0
NumStaticFields: 0
加载更多回复(335)

110,545

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧