는 나는이 코드를 작성했습니다 : True
입니다컨트롤의 태그 이름을 얻는 방법 (존재하는 경우)?
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
AlohaEnabled
경우, 나는 head
태그에 몇 가지 링크 및 스크립트를 추가 할 계획입니다. 어떤 종류의 Master
이 사용될 지 미리 알지 못하기 때문에 Controls
을 반복하고 TagName
이라는 필드를 리플렉션을 통해 찾습니다. field
에 값이있는 경우 "head"
과 비교하고 일치하면 aloha script
을 추가 할 예정입니다. 질문은 더 일반적이지만 다른 스크립트 나 다른 곳에도이 스크립트가 필요합니다. TagName
은 System.Web.UI.HtmlControls.HtmlControl의 필드입니다. 내 테스트 케이스의 0'th control
는 control.GetType
에
{Name = "HtmlHead" FullName = "System.Web.UI.HtmlControls.HtmlHead"}
를 반환합니다. System.Web.UI.HtmlControls.HtmlHead을 보면 System.Web.UI.HtmlControls.HtmlGenericControl을 상속받으며, 차례로 System.Web.UI.HtmlControls.HtmlContainerControl을 상속받으며 이는 HtmlControl
에서 상속받습니다. TagName은 Public
이므로, control.GetType.GetField("TagName")
에서 Return
"head"
으로 예상됩니다. 대신에 Nothing
을 반환합니다. 나는이 행동의 원인이 무엇일까?
편집 :
FloatingKiwi은 권리 문제 때문에 내가 속성의 목적은 어쨌든, 우리가 할 수있는 무엇 (그것을 발견하지 않았다, 나는 필드를 찾고 있었다, 그러나 이것은 속성입니다이었다 방법으로 작업을 해결하십시오.) 나는 그 동안-를 해결하려면를 사용하고 있습니다 :
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
나는 우수한 솔루션입니다 궁금하다 : 내 해결 방법 또는 재산 반사를 사용하여 검색 하시나요?
실제 값이 사용 얻으려면. 누군가 질문에 대한 설명을 해줄 수 있다면, 품질을 향상 시키거나 그것을 해결할 수 없으면 그것을 수정할 수 있습니다. –