I think this is all about readibility, not performance. It wouldn't matter so much, even with a virtual method call. This goes into microbenchmarking, in general.
On Fri, Dec 19, 2008 at 7:52 PM, MAMMON <[email protected]> wrote: > > I am not an expert on this, but here is what I've found. > > Considering the line: > if (typeof(IAuditableEntity).IsAssignableFrom(entity.GetType())) > > versus > > if (entity is IAuditableEntity) > > I set up a test project, created an IAuditableEntity interface, a > class that implements it, and issues the 2 different "if" statements. > Then I opened the assembly with reflector, to see the MSIL code that > is generated. > > > Using "entity is IAuditableEntity" results in: > > L_008b: ldloc.1 // loads local var at index 1 ("entity") > onto the evaluation stack > > L_008c: isinst is_test.IAuditableEntity // pops the top > var off the evaluation stack, testing if it's an instance of > IAuditableEntity > > > > Using "typeof(IAuditableEntity).IsAssignableFrom(entity.GetType()) > results in: > > L_00b3: ldtoken is_test.IAuditableEntity // Converts a > metadata token to its runtime representation, pushing it onto the > evaluation stack > > L_00b8: call class [mscorlib]System.Type [mscorlib] > System.Type::GetTypeFromHandle(valuetype [mscorlib] > System.RuntimeTypeHandle) // Gets the type of IAuditableEntity > from the handle we just pushed onto the stack > > L_00bd: ldloc.1 // pushes "entity" onto the stack (local > var at index 1) > > L_00be: callvirt instance class [mscorlib]System.Type [mscorlib] > System.Object::GetType() > > L_00c3: callvirt instance bool [mscorlib] > System.Type::IsAssignableFrom(class [mscorlib]System.Type) > > > > So using the "is" operator issues a single IL instruction: isint. > > Using if (typeof(IAuditableEntity).IsAssignableFrom(entity.GetType())) > results in: > + ldtoken instruction > + function call (System.Type::GetTypeFromHandle) > + virtual call (System.Object::GetType()) > + virtual call (System.Type::IsAssignableFrom) > > > Keep in mind that virtual calls are more expensive than regular > function calls as well. > > > I would use the "is" operator. It's a built in operator that issues a > single IL instruction. > > > > > > > > > On Dec 18, 3:09 pm, "Stefan Sedich" <[email protected]> wrote: > > Heh what a post to wake upto, thanks guys, I will look into using the > > events look 100 times easier. > > > > Cheers > > Stefan > > > > > > > > On Fri, Dec 19, 2008 at 7:00 AM, Will Shaver <[email protected]> > wrote: > > > Now you've just got me all confused and showing my ignorance. ;) > > > > > On Thu, Dec 18, 2008 at 1:55 PM, Tuna Toksöz <[email protected]> > wrote: > > > > >> and assuming you are not talking about managed c++ > > > > >> On Thu, Dec 18, 2008 at 11:52 PM, Tuna Toksöz <[email protected]> > wrote: > > > > >>> boxing in c++? > > >>> Are you sure that casting takes cycles? I think it doesn't, because > it is > > >>> the cmpilers job to cast. > > > > >>> On Thu, Dec 18, 2008 at 11: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! > > > > -- > > Stefan Sedich > > Software Developerhttp://weblogs.asp.net/stefansedich > > > -- 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 -~----------~----~----~----~------~----~------~--~---
