Skip to content

Instantly share code, notes, and snippets.

@urasandesu
Last active August 29, 2015 13:56
Show Gist options
  • Save urasandesu/9165977 to your computer and use it in GitHub Desktop.
Save urasandesu/9165977 to your computer and use it in GitHub Desktop.
using Urasandesu.Prig.Framework;
// IndirectableAttribute を使い、スタブに入れ替えたい Assembly にメタデータトークンを付与します。
// このメタデータトークンですが、問題になっている DateTime.Now の getter メソッドは get_Now という
// 名前の静的メソッドですので、そのトークンである 0x060002D2 を指定します。
// ※メタデータトークンは、対象の dll を ildasm 等で逆コンパイルしたり、MethodInfo.MetadataToken を
// 参照することによって確認することができます。
[assembly: Indirectable(0x060002D2)]
namespace System.Prig
{
public class PDateTime
{
public static class NowGet
{
// 対象のメソッドと同じシグネチャを持つデリゲートを選択します。
// デリゲートは、IndirectionDelegateAttribute 属性が付与されたものを選択することができます。
public static IndirectionFunc<DateTime> Body
{
set
{
// 以下で構成されるキーにデリゲートを登録します:
// - 対象のメソッドを含む Assembly の完全表示名。
// - 対象のメソッドのメタデータトークン。
var info = new IndirectionInfo();
info.AssemblyName = "mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";
info.Token = 0x060002D2;
var holder = LooseCrossDomainAccessor.GetOrRegister<IndirectionHolder<IndirectionFunc<DateTime>>>();
holder.AddOrUpdate(info, value);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment