I mean over gettype of course :) and btw, as is prefered, definitely. On Thu, Dec 18, 2008 at 11:38 PM, Tuna Toksöz <[email protected]> wrote:
> is is actually faster, i remember reading this in castle group probably. > > On Thu, Dec 18, 2008 at 11:37 PM, Daniel Fernandes < > [email protected]> wrote: > >> >> Usually "as" is preferred to "is" because the usage pattern of both >> requires most of the time to cast to the tested type. >> I had a look at Type.IsAssignableFrom with Reflector and it's a hell >> of a method nearly 50 lines long. >> No idea re performance as I never go anywhere near MSIL... >> >> On Dec 18, 9:25 pm, "Will Shaver" <[email protected]> wrote: >> > Yes, but with both Daniel and Tuna's examples are incomplete. >> > if (typeof(IAuditableEntity).IsAssignableFrom(entity.GetType())) { >> > ((IAuditableEntity) entity).Audit();} >> > >> > or >> > if (entity is IAuditableEntity) { >> > ((IAuditableEntity) entity).Audit(); >> > >> > } >> > >> > In BOTH cases you have to do TWO casts. In mine you only have to do one. >> I >> > learned in C++ where casting and boxing take cycles. >> > >> > -Will >> > >> > On Thu, Dec 18, 2008 at 1:17 PM, Tuna Toksöz <[email protected]> wrote: >> > > or >> > >> > > if (entity is IAuditableEntity) >> > > ? :) >> > >> > > On Thu, Dec 18, 2008 at 11:08 PM, Daniel Fernandes < >> > > [email protected]> wrote: >> > >> > >> Beside the point I know but : >> > >> if (typeof(IAuditableEntity).IsAssignableFrom(entity.GetType())) { >> > >> to >> > >> var auditable = entity as IAuditableEntity; >> > >> if (auditable != null) { >> > >> > >> isn't easier to read ? >> > >> > >> On Dec 18, 6:35 am, "Stefan Sedich" <[email protected]> wrote: >> > >> > Hello, >> > >> > >> > I am setting date last modified audit on an entity, I implement an >> > >> > IAuditableEntity interface and use a helper to set the date >> modified: >> > >> > >> > public override bool OnFlushDirty(object entity, object id, >> object[] >> > >> > currentState, object[] previousState, string[] propertyNames, >> > >> > global::NHibernate.Type.IType[] types) { >> > >> > >> > if >> > >> (typeof(IAuditableEntity).IsAssignableFrom(entity.GetType())) { >> > >> > >> > // If entity is auditable as this is only an update >> > >> > set the last modified date. >> > >> > SetValue(propertyNames, currentState, item => >> > >> > item.LastModified, DateTime.Now); >> > >> > >> > } >> > >> > >> > return false; >> > >> > } >> > >> > >> > Type safe helper: >> > >> > >> > public void SetValue<T>(string[] propertyNames, object[] state, >> > >> > Expression<System.Func<IAuditableEntity, T>> propertyExpression, T >> > >> > value) { >> > >> > >> > var memberExpression = propertyExpression.Body as >> > >> MemberExpression; >> > >> > if (memberExpression == null) >> > >> > throw new ArgumentException("The member expression >> was >> > >> > not a valid member expression."); >> > >> > >> > string name = memberExpression.Member.Name; >> > >> > int index = propertyNames.ToList().IndexOf(name); >> > >> > >> > if(index == -1) >> > >> > throw new >> > >> > InvalidOperationException(string.Format("Property {0} does no exist >> on >> > >> > entity.", name)); >> > >> > >> > state[index] = value; >> > >> > >> > } >> > >> > >> > Now the question is, I saw that you must set the current state and >> not >> > >> > the entity directly why would >> > >> > >> > var ent = entity as IAuditableEntity; >> > >> > ent.LastModified = DateTime.Now; >> > >> > >> > Not work properly or will it be fine to do this? >> > >> > >> > Thanks >> > >> > >> > -- >> > >> > Stefan Sedich >> > >> > Software Developerhttp://weblogs.asp.net/stefansedich >> > >> > > -- >> > > Tuna Toksöz >> > >http://tunatoksoz.com >> > >> > > Typos included to enhance the readers attention! >> >> >> > > > -- > Tuna Toksöz > http://tunatoksoz.com > > Typos included to enhance the readers attention! > > -- Tuna Toksöz http://tunatoksoz.com Typos included to enhance the readers attention! --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "nhusers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/nhusers?hl=en -~----------~----~----~----~------~----~------~--~---
