我正在使用OPC Foundation SDK在C#中实现OPC UA客户端,对于自定义数据类型,这似乎需要使用从SDK读取的数据动态创建类型。
我遇到的问题是,当动态创建的类型是另一个动态创建的类型的子类型时,当我尝试访问父类型的变量时,出现“错误CS0234:类型或名称空间名称'Plate '在名称空间'Application'中不存在(您是否缺少using指令或程序集引用?)。
代码很长,在任何情况下都没有OPC UA服务器就无法工作,因此发布可验证的示例很困难:(
任何想法都值得欢迎,因为我已经拔头发好几天了!
//beginning of type creation
var encodingId = foundCustomTypes[name];
var typeBuilder = moduleBuilder.DefineType(name.Name, TypeAttributes.Public | TypeAttributes.Class, typeof(GenericEncodeable));
typeBuilder.AddInterfaceImplementation(typeof(IEncodeable));
//BuildDynamicProperty(typeBuilder, NodeIdString, typeof(uint), (uint)encodingId.Identifier);
BuildDynamicProperty(typeBuilder, NodeIdString, typeof(uint), (uint)tempintref);
tempintref += 1;
BuildDynamicProperty(typeBuilder, NamespaceString, typeof(string), encodingId.NamespaceIndex.ToString());
//addition of known sub-type
{
typeType= typeType.Replace(TnsPrefixString, string.Empty );
var knownType = _createdTypeDictionary.Values.FirstOrDefault(t =>
t.FullName.Equals(typeType));
propertyType = knownType;
if (subsubChild.Attributes[LengthFieldString]?.Value != null)
{
propertyType = propertyType?.MakeArrayType();
}
}
if (propertyType != null)
{
BuildDynamicProperty(typeBuilder, typeName, propertyType, string.Empty, true);
}
//actual creation of type
Type createdType = typeBuilder.CreateType();
_createdTypeDictionary.Add(encodingId, createdType);
_session.Factory.AddEncodeableType(encodingId, createdType);
var test = Activator.CreateInstance(createdType);