我有:
类字段'可能'是:
问题:我在包含对象中没有List实例的实际对象。
一些示例代码来说明问题:
class Target
{
FieldInfo m_FieldInfo;
MethodInfo m_MethodInfo;
object m_Target;
public Target( string nameOfTargetField, object containingObject )
{
m_Target = containingObject;
Type type = containingObject.GetType();
BindingFlags bindingFlag = BindingFlags.Public | BindingFlags.Instance;
m_FieldInfo = type.GetField( nameOfTargetField, bindingFlag );
type = m_FieldInfo.FieldType;
m_MethodInfo = type.GetMethod( "Add" );
}
public void Set( object value )
{
if (m_MethodInfo != null) {
object[] parametersArray = new object[] { value };
// This obviously will not work, as m_Target is the containing object,
// no the List<T> instance contained within it:
m_MethodInfo.Invoke( m_Target, parametersArray );
} else if (m_FieldInfo != null) {
m_FieldInfo.SetValue( m_Target, value );
}
}
}
有什么建议吗?
谢谢!