Testing if a class has a certain Property

Testing if a class has a certain Property

Kevin.Bell
Advisor Advisor
445 Views
3 Replies
Message 1 of 4

Testing if a class has a certain Property

Kevin.Bell
Advisor
Advisor

Hi All!

 

I've put together a c# routine which lists parameters of elements the user selects.

 

Some of the parameters have a value which is an ElementId, rather than returning the ElementId (which means little to the user), I'd like the routine to return the name of the object the Id refers to.

 

To get the objects Human Readable Name I'm obtaining the element and then calling the Name Property:

 

 

pickparam is the a Parameter variable.
Element getob = doc.GetElement(pickparam.AsElementId());
String getresult = getob.Name;

 

This works fine - some of the time, as some of the Element the parameter refers to don't have a Name Property.

 

To stop the code failing, I'd like to test for this with an If statement, i.e.

 

 

if (getobName != null)
   {
   String getresult = getob.Name
   }
else
   {
   String getresult = "something else"
   }

 

 

The above code doesn't work, how can I test for a Classes Property such as Name?

 

(I hope the above is understandable!)

 

Thanks.

0 Likes
446 Views
3 Replies
Replies (3)
Message 2 of 4

RPTHOMAS108
Mentor
Mentor

Some of the derived classes have Name property hidden.

 

'The property inherited from element is hidden, a System.InvalidOperationException is thrown.'

 

You'll probably have to use system.reflection.memberinfo i.e Type.GetMembers or Type.GetMember(Name).

 

Then either iterate the list in the first method above or check the form of the member in the second.

 

Perhaps add an idea that the .ToString method should be implemented for Element. This would be good as.ToString is used by default for DisplayMember in WPF/WF bindings.

0 Likes
Message 3 of 4

Kevin.Bell
Advisor
Advisor

Hi,

 

Thanks for your response.

 

I added Type.GetMembers to my code but received 'The type name 'GetMembers' does not exist in the Type 'type'.

 

I added using System.Reflection to the beginning.

 

Is there something I'm missing?

 

Cheers.

0 Likes
Message 4 of 4

RPTHOMAS108
Mentor
Mentor

This link may be of use in terms of finding information about members of unknown types at runtime:

https://msdn.microsoft.com/en-us/library/k2w5ey1e(VS.110).aspx

 

I'm now thinking that ultimately this might not be that useful as looking through the derived classes of Element the Name property is available. I was initially thinking it was hidden similar to below. This would mean you could determine it from the memberinfo. However, if the Name property has not been hidden in such a way (and the derived class has been designed to throw an exception) then there may be no way of knowing.

 

    Public Class xx1
        Protected Overridable Property Name As String
        Public Property Number As Double
    End Class

    Public Class xx2
        Inherits xx1
        Private Shadows Property Name As String
    End Class
0 Likes