我已经定义:

[RdfSerializable]
public class SomeItem
{
   // Unique identificator of the resource
   [ResourceUri]
   public string ID { get; set; }

   [RdfProperty( true )]
   public string SomeData { get; set; }
}

and in some other class: 

[RdfProperty(true)]
public SomeItem[] MyTestProp
{
   get
   {
      return new SomeItem[] { new SomeItem() { ID="1", SomeData="test1" }, new SomeItem() { ID="2", SomeData = "test2" } };
   }
}

当我尝试序列化包含此自定义“MyTestProp”的类时,它给了我该消息:

你调用的对象是空的。

描述:在执行当前Web请求期间发生了一个未经治疗的例外。请查看堆栈跟踪,以获取有关该错误以及代码中何处的更多信息。

异常详细信息:系统.NullReferenceException:你调用的对象是空的。

我定义这些属性是错误的还是有一种特殊的方法将数组定义为自定义类?请注意,例如将数组序列化为字符串不会让我崩溃,但它正在工作。

完整来源:

using System;
using NC3A.SI.Rowlex;

[assembly: Ontology("ROWLEXtest1", "http://www.test.com/MyOntology")]

namespace ROWLEXtest1
{
   [RdfSerializable( HasResourceUri=false )]
   public class Item
   {
      [RdfProperty(true)]
      public string MyProp;
   }

   [RdfSerializable]
   public class AllItems
   {
      [RdfProperty(true)] public string mTitle;

      private int id = new Random().Next(0, 20);

      [ResourceUri]
      public string ResourceUri 
      {
         get { return "This " + id.ToString(); }
      }

      [RdfProperty(false)]
      public Item[] Items;
   }

   class Program
   {
      static void Main(string[] args)
      {
         var item = new AllItems();
         item.mTitle = "Hello World!";
         item.Items = new Item[] { new Item(){ MyProp = "test1" }, new Item(){ MyProp = "test2" } };

         var doc = Rdfizer.Serialize(item);

         System.Console.Out.Write(doc.ToString());
      }
   }
}

例外是:

system.nullReferenceException是未经用来的消息=“对象引用未设置为对象的实例。” source =“ nc3a.si.Rowlex” stacktrace:在NC3A.SI.ROWLEX.RDFPROPERTYATTRIBUTE.EXTRACTRANGE(成员Int32和MinCardinality,Int32&MaxCardinality)in nc3a.si.si.si.si.rowlex.rowlex.rdfpropertyattribute.extrange.extractrange.extrange(MembersInfo) y(rdfdocument doc,会员成员成员,rdfpropertyattribute属性,对象项目,字符串restryuri)atnc3a.si.si.rowlex.rowlex.rdfizer.appendsinglerdfllerdfserializableobject(rdfdocument doc,对象,对象项目),nc3a.si.si.rowlex.rowlex.rowlex.rdfizer.rdfizer.rdfizer.rocessitem(rdizer.processitem at rdfdfdocement at rdfdocults intriper) NC3A.SI.ROWLEX.ROWLEX.ROWLEX.ROWLEX.EXECUTESERIALIZIAD(IENUMERASE对象)在nc3a.si.si.rowlex.rowlex.rdfizer.serialize(iEnumerable对象,booleanSeriaLializebleObjects)nc3a.si.si.si.rowlex.rowlex.rdfizer.serialials.serialize.serialize.serialize aintrogment(booleansizebleObjects) (String[] args) in C:ROWLEXtest1ROWLEXtest1Program.cs:line 40 at System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() at System. threading.executionContext.run(executionContext executionContext,contextCallback callback,object状态)

有帮助吗?

解决方案

你所做的看起来不错,但有一个错误:RdfProperty 声明对于 MyTestProp 应该采用“false”,因为 MyTestProp 不是数据类型属性而是对象属性(它返回对象而不是文字)。

但是,我不确定这是您问题的根源。即使是这样,你也应该得到 带有有意义文本的体面错误消息,而不是愚蠢的 NullReferenceException. 。因此,我想尝试重现您的错误并提供修复(如果适用)。请您具体说明一下

  • 托管 MyTestProp 的类及其装饰,
  • 实例化该类的代码,以及
  • 用于序列化的代码。
  • 如果您应用程序集级属性(对于本体 - 命名空间映射),也请指出。

也许您可以考虑将您的代码示例发送给 [admin at rowlex.net]。

编辑:我可以重现该异常,这是 ROWLEX 中的一个错误。 修复后的 2.0.1 版本现在可以从 罗莱克斯 地点。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top