本文整理汇总了C#中Wrapper.WrapObjectReplacer方法的典型用法代码示例。如果您正苦于以下问题:C# Wrapper.WrapObjectReplacer方法的具体用法?C# Wrapper.WrapObjectReplacer怎么用?C# Wrapper.WrapObjectReplacer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Wrapper
的用法示例。
在下文中一共展示了Wrapper.WrapObjectReplacer方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RegisterPattern
private static void RegisterPattern(CustomPatternSchemaBase schema)
{
var automationPeerType = typeof(AutomationPeer);
// The only purpose of this method is to construct and correctly execute these lines of code:
//
// var wrapper = new Wrapper(schema.PatternProviderInterface);
// var wrapObject = new AutomationPeer.WrapObject(wrapper.WrapObjectReplacer);
// AutomationPeer.s_patternInfo[schema.PatternId]
// = new AutomationPeer.PatternInfo(schema.PatternId,
// wrapObject,
// (PatternInterface)schema.PatternId);
// AutomationPattern.Register(schema.PatternGuid, schema.PatternName);
//
// The problem here is that AutomationPeer.WrapObject, AutomationPeer.s_patternInfo, AutomationPattern.Register
// and AutomationPeer.PatternInfo are not public, so we need some hardcore reflection.
// Now, to be very clear, casting patternId to PatternInterface is not totally correct,
// but customly registered patterns get IDs near 50000 and max PatternInterface value is
// something about 20, so they won't intersect ever. On the other hand, after several hours
// studying AutomationPeer sources it seems unfeasible to get what we need in other way
// because AutomationPeer wasn't written with extensibility in mind.
var patternInfoHashtableField = automationPeerType.GetField("s_patternInfo", BindingFlags.NonPublic | BindingFlags.Static);
// from AutomationPeer.cs: private delegate object WrapObject(AutomationPeer peer, object iface);
var wrapObjectDelegateType = automationPeerType.GetNestedType("WrapObject", BindingFlags.NonPublic);
var wrapper = new Wrapper(schema.PatternProviderInterface);
var wrapObjectReplacerMethodInfo = ReflectionUtils.GetMethodInfo(() => wrapper.WrapObjectReplacer(null, null));
var wrapObject = Delegate.CreateDelegate(wrapObjectDelegateType, wrapper, wrapObjectReplacerMethodInfo);
// from AutomationPeer.cs:
//private class PatternInfo
//{
// internal int Id;
// internal AutomationPeer.WrapObject WrapObject;
// internal PatternInterface PatternInterface;
// internal PatternInfo(int id, AutomationPeer.WrapObject wrapObject, PatternInterface patternInterface)
// {
// this.Id = id;
// this.WrapObject = wrapObject;
// this.PatternInterface = patternInterface;
// }
//}
var patternInfoType = automationPeerType.GetNestedType("PatternInfo", BindingFlags.NonPublic);
var patternInfoTypeCtor = patternInfoType.GetConstructor(
BindingFlags.NonPublic | BindingFlags.Instance,
binder: null,
types: new[] {typeof(int), wrapObjectDelegateType, typeof(PatternInterface)},
modifiers: null);
var patternInfo = patternInfoTypeCtor.Invoke(new object[] {schema.PatternId, wrapObject, (PatternInterface)schema.PatternId});
var automationPatternType = typeof(AutomationPattern);
var registerMethod = automationPatternType.GetMethod("Register", BindingFlags.NonPublic | BindingFlags.Static);
using (Dispatcher.CurrentDispatcher.DisableProcessing())
{
var patternInfoHashtable = (Hashtable)patternInfoHashtableField.GetValue(null);
if (patternInfoHashtable.Contains(schema.PatternId)) return;
patternInfoHashtable[schema.PatternId] = patternInfo;
registerMethod.Invoke(null, new object[] {schema.PatternGuid, schema.PatternName});
}
}