0

我在 C# 中提供了一个(简单的)代码示例,我现在正尝试将其转换为 IronPython:

Clearcore2.Licensing.LicenseKeys.Keys = new[] {
          @"<?xml version=""1.0"" encoding=""utf-8""?>
            <license_key>
                <company_name>Company </company_name>
                <product_name>ProcessingFramework</product_name>
                <features>WiffReaderSDK</features>
                <key_data>
                    2F923C31D1E7E16B191EF2F87DCA7F15831A3F18DED11E05582A98822==
                </key_data>
            </license_key>"};

实际的许可证密钥存储在文件“许可证”中。我跑:

Licensing.LicenseKeys.Keys = Array[str]([open('license').read()])

但得到

System.TypeInitializationException: The type initializer for 'Clearcore2.Data.WiffReader.WiffSampleRun' threw an exception. ---> Clearcore2.Licensing.LicenseKeyFormatException: License key can't be parsed! ---> System.ArgumentNullException: String reference not set to an instance of a String.
Parameter name: s
    at System.Text.Encoding.GetBytes(String s)
    at Clearcore2.Licensing.XmlKeyConversions.LoadXmlDocumentForString(String text)
    at Clearcore2.Licensing.XmlKeyConversions.LoadLicenseKey(String licenseKey, String& companyName, String& productName, String& features)
    --- End of inner exception stack trace ---
    at Clearcore2.Licensing.XmlKeyConversions.LoadLicenseKey(String licenseKey, String& companyName, String& productName, String& features)
    at Clearcore2.Licensing.Verifier.VerifyLicenseKey(String publicKey, String licenseKey)
    at Clearcore2.Utility.Licensing.Protection.Verify()
    at Clearcore2.Data.WiffReader.WiffSampleRun..cctor()
    --- End of inner exception stack trace ---
    at Clearcore2.Data.WiffReader.WiffSampleRun..ctor(WiffFile wiffFile, WiffSampleSubTree wiffSampleSubTree, Int32 sampleIndex)
    at Clearcore2.Data.WiffReader.WiffSampleSubTree.GetWiffSampleRun(Int32 sampleIndex)
    at Clearcore2.Data.AnalystDataProvider.AnalystWiffDataProvider.GetWiffSampleRun(String wiffPath, Int32 sampleIndex)
    at Clearcore2.Data.AnalystDataProvider.SampleImplementation.PopulateSampleInfo()
    at Clearcore2.Data.AnalystDataProvider.SampleImplementation..ctor(SampleLocator locator, AnalystWiffDataProvider dataProvider)
    at Clearcore2.Data.AnalystDataProvider.AnalystWiffDataProvider.CreateSampleFromLocator(SampleLocator locator)
    at Clearcore2.Data.AnalystDataProvider.BatchImp.GetSample(Int32 index)
    at Microsoft.Scripting.Interpreter.FuncCallInstruction`3.Run(InterpretedFrame frame)
    at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)
    at Microsoft.Scripting.Interpreter.LightLambda.Run4[T0,T1,T2,T3,TRet](T0 arg0, T1 arg1, T2 arg2, T3 arg3)
    at System.Dynamic.UpdateDelegates.UpdateAndExecute3[T0,T1,T2,TRet](CallSite site, T0 arg0, T1 arg1, T2 arg2)
    at Microsoft.Scripting.Interpreter.DynamicInstruction`4.Run(InterpretedFrame frame)
    at Microsoft.Scripting.Interpreter.Interpreter.Run(InterpretedFrame frame)

我也试过

Licensing.LicenseKeys.Keys = Array[str](open('license').read())

但效果相同。

实际上 Licensing.LicenseKeys.Keys 需要 Array[str],但我不确定 C# 示例中的 @ 是什么意思。

IronPython 中 C# 示例的等效代码是什么?

4

1 回答 1

0

IronPython 中的一个近似等价物是

Clearcore2.Licensing.LicenseKeys.Keys = Array[str]([r"""<?xml version=""1.0"" encoding=""utf-8""?>
            <license_key>
                <company_name>Company </company_name>
                <product_name>ProcessingFramework</product_name>
                <features>WiffReaderSDK</features>
                <key_data>
                    2F923C31D1E7E16B191EF2F87DCA7F15831A3F18DED11E05582A98822==
                </key_data>
            </license_key>"""])

C# 中的@指定逐字字符串文字。这意味着不处理转义序列等,并且字符串可能跨越多行。原始的三引号字符串应该是等效的文字。

由于您读取文件的编码和/或模式,您的初始代码可能会失败。根据文件数据应该发生的情况,您可能希望指定另一种打开文件的模式(例如b表示binary)或使用类似File.ReadAllText的 .NET BCL 函数,您也可以从 C# 中检查该函数。

于 2013-07-18T12:20:49.303 回答