Orchard 1.3 , check if object property exists

Topics: Troubleshooting
Apr 16, 2012 at 5:22 PM

Hi, I just want to know if it is possible to check if an attribute/property exists in a object.

Something like:

if(widgetPart.RenderTitle.GetType().ToString() != null) {...}




Its giving me: 'Orchard.Widgets.Models.WidgetPart' does not contain a definition for 'RenderTitle' and no extension method 'RenderTitle'.

Apr 16, 2012 at 5:58 PM


But why don't you just go check the type yourself and don't use members that don't exist and so won't even compile?

May 4, 2012 at 4:04 PM

Thanks for your reply.

Can you please show me a example or guide me how to use the system.reflection ?


May 4, 2012 at 4:17 PM

I just gave you the link to the documentation.

May 4, 2012 at 4:25 PM

I know :P the problem is how to use it, i will search a little more.

Thank you.

May 4, 2012 at 4:29 PM

Well the point of documentation is usually to tell you how to use it ;)

May 4, 2012 at 5:06 PM
Edited May 4, 2012 at 5:07 PM

I get my solution like this: 

var renderTitleObj = ((IContent)Model.ContentItem).As<WidgetPart>();

System.Reflection.PropertyInfo propInfoSrcObj = renderTitleObj.GetType().GetProperty("RenderTitle");

if (propInfoSrcObj != null) { renderTitle = Convert.ToBoolean(propInfoSrcObj.GetValue(renderTitleObj, null)); }

Thanks for the tip.