111,046
社区成员
发帖
与我相关
我的任务
分享
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();
}
}
//-------------- 内存布局 ----------
// 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();
}
}
}
}
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;
}
}
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();
}
}
class MyClass
{
static void Main()
{
object o = new object();
}
}