【问题标题】:Using RdfProperty of array to custom class使用数组的 RdfProperty 自定义类
【发布时间】:2009-07-13 21:14:56
【问题描述】:

我已经定义了:

[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”的类时,它给了我这条消息:

对象引用未设置为 对象的实例。

描述:未处理的异常 在执行过程中发生 当前的网络请求。请查看 堆栈跟踪以获取有关的更多信息 错误及其起源 编码。

异常详情: System.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 是 未处理的 Message="对象引用 未设置为对象的实例。” 来源="NC3A.SI.Rowlex" 堆栈跟踪: 在 NC3A.SI.Rowlex.RdfPropertyAttribute.ExtractRange(成员信息 memberInfo, Int32& minCardinality, Int32& 最大基数) 在 NC3A.SI.Rowlex.RdfPropertyAttribute.ExtractRange(成员信息 会员信息) 在 NC3A.SI.Rowlex.Rdfizer.AppendProperty(RdfDocument doc, MemberInfo memberInfo, RdfPropertyAttribute 属性,对象 项目,字符串 resourceUri) 在 NC3A.SI.Rowlex.Rdfizer.AppendSingleRdfSerializableObject(RdfDocument 文档,对象项目) 在 NC3A.SI.Rowlex.Rdfizer.ProcessItem(RdfDocument 文档,对象项,字符串 [] rangeTypeUris) 在 NC3A.SI.Rowlex.Rdfizer.ExecuteSerialization(IEnumerable 对象) 在 NC3A.SI.Rowlex.Rdfizer.Serialize(IEnumerable 对象,布尔值 容忍UnserializebleObjects) 在 NC3A.SI.Rowlex.Rdfizer.Serialize(对象 物品) 在 ROWLEXtest1.Program.Main(String[] 参数)在 C:\ROWLEXtest1\ROWLEXtest1\Program.cs:行 40 在 System.AppDomain._nExecuteAssembly(程序集 程序集,字符串 [] 参数) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ExecutionContext.Run(ExecutionContext 执行上下文,上下文回调 回调,对象状态) 在 System.Threading.ThreadHelper.ThreadStart() 内部异常:

【问题讨论】:

  • 我已经添加了完整的源代码和异常信息。我希望这能帮助您检查问题。

标签: c# .net rdf rowlex semweb


【解决方案1】:

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

但是,我不确定这是您问题的根源。即使是,你也应该得到a decent error message with meaningful text instead of silly NullReferenceException。因此,我想尝试重现您的错误并在适用时提供修复。能否请您指定

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

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

编辑: 我可以重现异常,这是 ROWLEX 中的一个错误。 现在可以从ROWLEX站点下载固定的2.0.1版本。

【讨论】:

    猜你喜欢
    • 2019-05-17
    • 2015-01-13
    • 2015-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-20
    • 2021-01-07
    • 1970-01-01
    相关资源
    最近更新 更多