我使用nhibernate拦截器比较旧状态的值和实体属性的当前状态,但是某些属性的类型为ICollection
,所以任何人都可以指导我如何检查对象是否属于输入ICollection
这是我的代码
public void OnPostUpdate(NHibernate.Event.PostUpdateEvent @event)
{
var entityToAudit = @event.Entity as IAuditable;
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AuditLog.txt");
using (StreamWriter sw = File.AppendText(path))
{
for (int i = 0; i < @event.OldState.Length; i++)
{
string propertyName = @event.Persister.PropertyNames[i];
if (@event.OldState[i] != null)
{
if (!@event.OldState[i].Equals(@event.State[i]))
{
sw.WriteLine("the value of "+ propertyName + " has been changed from " + @event.OldState[i] + " to " + @event.State[i]);
}
}
else
{
if (@event.State[i] != null)
{
sw.WriteLine("the value of "+ propertyName + " has been changed from being empty to " + @event.State[i]);
}
}
}
}
}
答案 0 :(得分:1)
您有多个选项可以执行此操作,使用 或使用作为进行空检查:
if (obj is ICollection){
//your logic
}
或者,如果您稍后需要将该对象作为ICollection,我建议使用作为:
var icoll = obj as ICollection
if (icoll != null){
//use icoll
//icoll.Something();
}
答案 1 :(得分:0)
您可以使用简单的 检查类型 像这样:
var obj = getObject();
if(obj is TypeYouWant)
doSomething();
祝你好运