我已经编写了一些例程,用于在C#中对文本进行语音转换。这些例程需要一堆定义好的转换规则(搜索字符串/替换字符串)。想法是将这些规则存储为程序集中的嵌入式资源,然后从中读取规则。反序列化的起点如下

public static phonet42n.Core.Rules Deserialize(phonet42n.Core.Rules.Ressources ressource)
{
    string ressourceName;
    phonet42n.Core.Rules returnValue;
    System.Xml.XmlReader reader;
    System.Xml.Serialization.XmlSerializer xmlSerializer;
    phonet42n.Core.SerializableRules serializeableRules;

    returnValue = new phonet42n.Core.Rules();

    switch (ressource)
    {
        case Ressources.German_01:
            ressourceName = RESSOURCE_XML_GERMAN_01;
            break;
        case Ressources.German_02:
            ressourceName = RESSOURCE_XML_GERMAN_02;
            break;
        default:
            ressourceName = RESSOURCE_XML_GERMAN_01;
            break;
    }

    using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream(ressourceName))
    {
        using (reader = System.Xml.XmlReader.Create(stream))
        {
            xmlSerializer = new System.Xml.Serialization.XmlSerializer(typeof(phonet42n.Core.SerializableRules));
            serializeableRules = (phonet42n.Core.SerializableRules)xmlSerializer.Deserialize(reader);
        }
    }

    foreach (phonet42n.Core.Rule entry in serializeableRules.Rules)
    {
        if (entry.SearchString != null && entry.SearchString.Length > 0)
        {
            returnValue.Add(entry.Index, entry);
        }
    }
    return returnValue;
}


在常规可执行文件中执行时,该应用程序可以正常运行。

在SQL Server中执行注册的函数时,出现以下错误:

SELECT [dbo].[Phonet42n]('mayer', 1)


生产...


  Meldung 6522,Ebene 16,状态1,Zeile 22
  .NET Framework-Fehler beimAusführender benutzerdefinierten例行程序benutzerdefinierten汇总了“ Phonet42n”:
  System.InvalidOperationException:Fehler im XML-Dokument(3,4)。 ---> System.MethodAccessException:Fehler beim Versuch der Methode“ Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read8_SerializableRules(Boolean,Boolean)”,方法是“ phonet42n.Core.Rule..ctor()” zuzugreifen。
  System.MethodAccessException:
  bei System.RuntimeTypeHandle.CreateInstance(RuntimeType类型,布尔publicOnly,布尔noCheck,布尔&canBeCached,RuntimeMethodHandleInternal&ctor,布尔&bNeedSecurityCheck)
  bei System.RuntimeType.CreateInstanceSlow(布尔publicOnly,布尔skipCheckThis,布尔fillCache,StackCrawlMark和stackMark)
  bei System.Activator.CreateInstance(类型,布尔值非公共)
  bei System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr,活页夹活页夹,Object [] args,CultureInfo文化,Object [] activationAttributes,StackCrawlMark&stackMark)
  bei System.Activator.CreateInstance(类型类型,BindingFlags bindingAttr,活页夹装订器,Object [] args,CultureInfo文化,Object [] activationAttributes)
  bei System.Activator.CreateInstance(类型类型,BindingFlags bindingAttr,活页夹活页夹,Object [] args,CultureInfo文化)
  bei Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read8_SerializableRules(Boolean isNullable,Boolean checkType)
  bei Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReader1.Read19_Rules()
  System.InvalidOperationException:
  bei System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader,字符串encodingStyle,XmlDeserializationEvents事件)
  bei System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader)
  bei phonet42n.Core.Rules.Deserialize(资源ressource)
  bei phonet42n.Core.Rules..ctor(资源ressource,字符字符)
  bei phonet42n.Core.HashTable..ctor(资源ressource)
  bei phonet42n.Core.Match..ctor(重新...


任何的想法?

最佳答案

只要有安全例外,您可以首先尝试将Assembly设置为PERMISSION_SET = EXTERNAL_ACCESS,如果这样做不起作用,则可以尝试UNSAFE。但是,如果尝试动态加载程序集,则即使标记为UNSAFE的程序集也应禁止这样做。

由于这里的问题是要包括一套规则,因此可以在另一届大会上做到。然后,主程序集可以引用包含规则的规则,而您只需先将包含规则的规则加载到SQL Server中即可。这将使两个程序集都保持标记为SAFE

当然,如果没有迫切需要将规则分开,那么您也可以将它们直接放入类的Collection中。

关于c# - CLR函数中的反序列化失败,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/39750330/

10-11 01:25