所以这看起来非常基本,但我无法让它发挥作用.我有一个Object,我使用反射来获取它的公共属性.其中一个属性是静态的,我没有运气.
Public Function GetProp(ByRef obj As Object, ByVal propName as String) as PropertyInfo
Return obj.GetType.GetProperty(propName)
End Function
Run Code Online (Sandbox Code Playgroud)
上面的代码适用于Public Instance属性,到目前为止我只需要它.据说我可以使用BindingFlags来请求其他类型的属性(私有,静态),但我似乎找不到合适的组合.
Public Function GetProp(ByRef obj As Object, ByVal propName as String) as PropertyInfo
Return obj.GetType.GetProperty(propName, Reflection.BindingFlags.Static Or Reflection.BindingFlags.Instance Or Reflection.BindingFlags.Public)
End Function
Run Code Online (Sandbox Code Playgroud)
但是,请求任何静态成员返回任何内容..NET反射器可以很好地看到静态属性,所以很明显我在这里遗漏了一些东西.
我正在使用.Net中的Timespans进行一些数学计算,偶尔总结会产生负时间跨度.当我显示结果时,我无法格式化它以包含负面指示器.
Dim ts as New Timespan(-10,0,0)
ts.ToString()
Run Code Online (Sandbox Code Playgroud)
这将显示"-10:00:00",这很好,但我不想显示秒,所以尝试了这个.
ts.ToString("hh\:mm")
Run Code Online (Sandbox Code Playgroud)
这将返回"10:00"并从前面删除" - ",这是问题的关键.我目前的解决方案是:
If(ts < TimeSpan.Zero, "-", "") & ts.ToString("hh\:mm")
Run Code Online (Sandbox Code Playgroud)
但我希望只使用格式字符串来完成同样的工作.