2009-07-13 27 views
2

我已经定义:使用RdfProperty阵列来定制类

[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”之类的,它给了我这一信息:

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

上午在定义这些属性时我错了,还是有一种特殊的方法来定义数组到自定义类?请注意,例如序列化字符串不会以这种方式崩溃,但它正在工作。

整个源代码:

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 was unhandled Message="Object reference not set to an instance of an object." Source="NC3A.SI.Rowlex" StackTrace: at NC3A.SI.Rowlex.RdfPropertyAttribute.ExtractRange(MemberInfo memberInfo, Int32& minCardinality, Int32& maxCardinality) at NC3A.SI.Rowlex.RdfPropertyAttribute.ExtractRange(MemberInfo memberInfo) at NC3A.SI.Rowlex.Rdfizer.AppendProperty(RdfDocument doc, MemberInfo memberInfo, RdfPropertyAttribute attribute, Object item, String resourceUri) at NC3A.SI.Rowlex.Rdfizer.AppendSingleRdfSerializableObject(RdfDocument doc, Object item) at NC3A.SI.Rowlex.Rdfizer.ProcessItem(RdfDocument doc, Object item, String[] rangeTypeUris) at NC3A.SI.Rowlex.Rdfizer.ExecuteSerialization(IEnumerable objects) at NC3A.SI.Rowlex.Rdfizer.Serialize(IEnumerable objects, Boolean tolerateUnserializebleObjects) at NC3A.SI.Rowlex.Rdfizer.Serialize(Object item) at ROWLEXtest1.Program.Main(String[] args) in C:\ROWLEXtest1\ROWLEXtest1\Program.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 state) at System.Threading.ThreadHelper.ThreadStart() InnerException:

+0

我已添加完整源代码和异常信息。我希望这能帮助你检查问题。 – 2009-07-14 16:42:22

回答

2

做过什么,看起来不错,但有一个错误:为MyTestProp不是 RdfProperty声明应采取 “假” 为MyTestProp数据类型属性但对象属性(它返回对象而不是文字)。

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

  • 类及其装饰承载MyTestProp,
  • ,你实例化类的代码,并
  • 您使用序列化的代码。
  • 如果您应用程序集级属性(对于本体 - 名称空间映射),请指出。

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

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

+0

谢谢!我会测试它。 – 2009-07-15 16:02:07