gpt4 book ai didi

c# - 使用数组的 RdfProperty 自定义类

转载 作者:太空宇宙 更新时间:2023-11-03 11:54:13 25 4
gpt4 key购买 nike

我已经定义了:

[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());
}
}
}

异常(exception)是:

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:

最佳答案

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

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

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

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

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

关于c# - 使用数组的 RdfProperty 自定义类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1122106/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com