我正在使用Reflection类来获取某个对象中的所有字段.然而,我的问题是,当字段在普通类中时,它可以正常工作,例如:
class test
{
string test1 = string.Empty;
string test2 = string.Empty;
}
Run Code Online (Sandbox Code Playgroud)
在这里我得到test1和test2,我的问题是我使用抽象,因此结合了几个类.
我有类似的东西:
class test3 : test2
{
string test4 = string.Empty;
string test5 = string.Empty;
}
class test2 : test1
{
string test2 = string.Empty;
string test3 = string.Empty;
}
class test1
{
string test0 = string.Empty;
string test1 = string.Empty;
}
Run Code Online (Sandbox Code Playgroud)
但是,当我运行它时,我不会从中获取字段GetType().GetFields(BindingFlag.Default).
这些领域的每个人都有get; set;附属于它的财产.当我运行代码时,我将属性一直返回到test1但不是实际的字段.
这是我试图获取字段的代码:
FieldInfo[] fields = Obj.GetType().GetFields(BindingFlags.Default);
foreach (FieldInfo field in fields)
Run Code Online (Sandbox Code Playgroud)
我也尝试过:
FieldInfo[] fields = Obj.GetType().GetFields(BindingFlags.Public
| BindingFlags.Instance
| …Run Code Online (Sandbox Code Playgroud) 我想从NHibernate的SqlClientBatchingBatcher类继承,就像这样(从带有加密触发器的TooManyRowsAffectedException获取的代码 ):
public class NonBatchingBatcherWithoutVerification : SqlClientBatchingBatcher
{
public NonBatchingBatcherWithoutVerification(ConnectionManager connectionManager, IInterceptor interceptor) : base(connectionManager, interceptor)
{}
protected override void DoExecuteBatch(IDbCommand ps)
{
log.DebugFormat("Executing batch");
CheckReaders();
Prepare(currentBatch.BatchCommand);
if (Factory.Settings.SqlStatementLogger.IsDebugEnabled)
{
Factory.Settings.SqlStatementLogger.LogBatchCommand(currentBatchCommandsLog.ToString());
currentBatchCommandsLog = new StringBuilder().AppendLine("Batch commands:");
}
int rowsAffected = currentBatch.ExecuteNonQuery();
// Removed the following line
//Expectations.VerifyOutcomeBatched(totalExpectedRowsAffected, rowsAffected);
currentBatch.Dispose();
totalExpectedRowsAffected = 0;
currentBatch = new SqlClientSqlCommandSet();
}
}
Run Code Online (Sandbox Code Playgroud)
请注意这里的方法中访问的一些成员(如currentBatch或totalExpectedRowsAffected).
好吧,事实证明这些成员在当前NHibernate 3.3源代码的超类中实际上是私有的.那么如何在不复制整个事物的情况下有效地继承类呢?这是类的未经修改的NHibernate代码:
public class SqlClientBatchingBatcher : AbstractBatcher
{
private int _batchSize;
private int _totalExpectedRowsAffected;
private SqlClientSqlCommandSet _currentBatch;
private StringBuilder …Run Code Online (Sandbox Code Playgroud) 我遇到了一个问题.
在Unity中,我想反思一个私人领域.但我总是为fieldinfo获取null.我究竟做错了什么?
public abstract class _SerializableType
{
[SerializeField] private string name;
}
// because I am using a CustomPropertyDrawer for all inherited from _SerializeType
public class SerializableType<T> : _SerializableType { }
public class SerializableType : _SerializableType { }
[Serializable] public class CTech : SerializableType<_CouplingTechnology> { }
Run Code Online (Sandbox Code Playgroud)
所以使用这种方法实际上应该工作.
// type is CTech
// propertyPath in that case is "name"
FieldInfo info = type.GetField(propertyPath, BindingFlags.Instance
| BindingFlags.Public | BindingFlags.NonPublic);
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?
我在具有自己的CustomInspector的托管库中调用此方法.所以它反映到每个领域并计算如何显示它.AppDomain是完全信任的.我不知道还有什么可能是重要的......
c# ×3
reflection ×2
bindingflags ×1
inheritance ×1
managed ×1
members ×1
nhibernate ×1
overriding ×1
private ×1