-1

I have written this code:

    If (AlohaEnabled) Then
        Dim head As Control = Nothing
        For Each control In Master.Controls
            Dim field = control.GetType.GetField("TagName")
            If ((field IsNot Nothing) AndAlso (field.GetValue(control).Equals("head"))) Then
                'Add aloha scripts
            End If
        Next
    End If

If AlohaEnabled is True, then I intend to add some links and scripts to the head tag. I do not know in advance what kind of Master will be used, therefore I iterate its Controls and look for a field called TagName through reflection. If field has a value, then I compare it to "head" and if it is a match, then I intend to add aloha scripts (the question is more general though, I could need this for different scripts as well or somewhere else). TagName is a field of System.Web.UI.HtmlControls.HtmlControl. The 0'th control in my test case returns

{Name = "HtmlHead" FullName = "System.Web.UI.HtmlControls.HtmlHead"}

on control.GetType. If we look at System.Web.UI.HtmlControls.HtmlHead, we see that it inherits System.Web.UI.HtmlControls.HtmlGenericControl, which on its turn inherits System.Web.UI.HtmlControls.HtmlContainerControl, which inherits from HtmlControl. Since TagName is Public, I would expect control.GetType.GetField("TagName") to Return "head". Instead of that, it returns Nothing. I wonder what is the cause of this behavior?

EDIT:

FloatingKiwi is right, the problem was that I was searching for a field, but this is a property, therefore I did not find it (what is the purpose of properties anyway, we can resolve their tasks with methods). I have used a work-around in the meantime:

        For Each control In Master.Controls
            If (TypeOf control Is System.Web.UI.HtmlControls.HtmlControl) Then
                Dim htmlControl = CType(control, System.Web.UI.HtmlControls.HtmlControl)
                If (htmlControl.TagName.Equals("head")) Then
                    'Add aloha scripts
                End If
            End If
        Next

I wonder which is the superior solution: my work-around, or property searching using reflection?

Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
  • I have observed that the question was down-voted. If someone could explain the problem with the question, I could edit it to improve its quality or remove it if it is unsalvageable. – Lajos Arpad Aug 14 '16 at 11:09

1 Answers1

1

It's a Property not a Field. Use

Dim propInfo = control.GetType.GetProperty("TagName") instead.

That will return a PropertyInfo object. To get the actual value use

Dim result = propInfo .GetValue(control, Nothing)

FloatingKiwi
  • 4,408
  • 1
  • 17
  • 41
  • control.GetType.GetProperty("TagName").GetValue(control, Nothing) solves the problem indeed, but If (TypeOf control Is System.Web.UI.HtmlControls.HtmlControl) Then Dim htmlControl = CType(control, System.Web.UI.HtmlControls.HtmlControl) works well as well. I wonder which code is superior, the actual answer to my question or the work-around I implemented instead. – Lajos Arpad Aug 08 '16 at 13:04
  • Please take a look at the question, which was edited. As you can see I have implemented a work-around and wonder whether it is better or worse than the solution you have shown (which is the answer to the question). – Lajos Arpad Aug 08 '16 at 13:08
  • 1
    It's always better to go with the strongly typed approach rather than using reflection. – FloatingKiwi Aug 08 '16 at 13:34
  • Thanks, mate, nice answer, nice comment :) – Lajos Arpad Aug 08 '16 at 13:40