【问题标题】:Edit file metadata - Code is fine in VB but not C#编辑文件元数据 - 代码在 VB 中很好,但在 C# 中不行
【发布时间】:2013-10-02 13:49:56
【问题描述】:

这是目前使用的VB.NET方法,效果很好。

Private Sub editMetadata(ByRef bmp1 As Bitmap, ByVal intTitleId As Integer, ByVal strTitle As String)

        Dim ci As System.Reflection.ConstructorInfo = _
                       GetType(PropertyItem).GetConstructor(BindingFlags.NonPublic Or _
                       BindingFlags.Instance Or BindingFlags.[Public], Nothing, New Type() {}, Nothing)

        Dim outPropertyItem As PropertyItem = DirectCast(ci.Invoke(Nothing), PropertyItem)

        outPropertyItem.Id = intTitleId

        ' Type=1 means Array of Bytes. 
        outPropertyItem.Type = 1
        outPropertyItem.Len = strTitle.Length

        outPropertyItem.Value = Encoding.Unicode.GetBytes(strTitle)

        bmp1.SetPropertyItem(outPropertyItem)

End Sub


现在在 C# 中,我在这个方法的第一行遇到了障碍。

System.Reflection.ConstructorInfo ci = 
      GetType(PropertyItem).GetConstructor( BindingFlags.NonPublic ||
      BindingFlags.Instance || BindingFlags.[Public], null, new Type() {}, null);


在将语法从 VB 转换为 C#(我熟悉的)之后,我在这一节上仍然有 6 个错误。



所以,这个问题可以通过几种方式解决...

上面显示的代码的正确 C# 语法是什么。

或者..

如何在 C# 中正确编辑文件元数据。

感谢您的帮助!

【问题讨论】:

  • 使用 |而不是 ||在 BindingFlags
  • 啊,消除了错误 #3。谢谢你:) !!

标签: c# .net vb.net reflection metadata


【解决方案1】:
System.Reflection.ConstructorInfo ci = 
      typeof(PropertyItem).GetConstructor( BindingFlags.NonPublic |
      BindingFlags.Instance | BindingFlags.Public, null, new Type[] {}, null);

【讨论】:

  • 我已经删除了 BindingFlags.Public 周围的 [],然后这一切都是正确的。谢谢!!如果允许,将在 10 分钟内标记为答案。
猜你喜欢
  • 2014-07-19
  • 2016-01-09
  • 2016-01-21
  • 2012-11-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-31
  • 1970-01-01
相关资源
最近更新 更多