customaccessdo.cs
来自「NHibernate NET开发者所需的」· CS 代码 · 共 125 行
CS
125 行
using System.Collections;
using System.Reflection;
using NHibernate.Engine;
using NHibernate.Properties;
namespace NHibernate.Test.NHSpecificTest.NH251
{
/// <summary>
/// The DictionaryAccessor access strategy uses this interface to
/// access properties
/// </summary>
public interface IDynamicFieldContainer
{
IDictionary Fields { get; }
}
// A component class
public class Name
{
private string first;
private string last;
}
/// <summary>
/// A domain object for the test case
/// </summary>
public class CustomAccessDO : IDynamicFieldContainer
{
public IDictionary dynamicFields = new Hashtable(); // may contain components
public int id;
public IDictionary Fields
{
get { return dynamicFields; }
}
}
/// <summary>
/// Custom access strategy that uses IDynamicFieldContainer to get/set property values
/// </summary>
public class DictionaryAccessor : IPropertyAccessor
{
public IGetter GetGetter(System.Type theClass, string propertyName)
{
return new CustomGetter(theClass, propertyName);
}
public ISetter GetSetter(System.Type theClass, string propertyName)
{
return new CustomSetter(propertyName);
}
public bool CanAccessTroughReflectionOptimizer
{
get { return false; }
}
public class CustomGetter : IGetter
{
private System.Type theClass;
private string propertyName;
public CustomGetter(System.Type theClass, string propertyName)
{
this.theClass = theClass;
this.propertyName = propertyName;
}
public object Get(object target)
{
IDynamicFieldContainer container = (IDynamicFieldContainer) target;
return container.Fields[propertyName];
}
public System.Type ReturnType
{
get { return theClass; }
}
public string PropertyName
{
get { return propertyName; }
}
public MethodInfo Method
{
get { return null; }
}
public object GetForInsert(object owner, IDictionary mergeMap, ISessionImplementor session)
{
return Get(owner);
}
// Optional operation (return null)
}
public class CustomSetter : ISetter
{
private string propertyName;
public CustomSetter(string propertyName)
{
this.propertyName = propertyName;
}
public void Set(object target, object value)
{
IDynamicFieldContainer container = (IDynamicFieldContainer) target;
container.Fields[propertyName] = value;
}
public string PropertyName
{
get { return propertyName; }
}
public MethodInfo Method
{
get { return null; }
} // Optional operation
}
}
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?