- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一些 .NET 互操作代码,我在其中设法加载对象并读取属性,但是在设置对象的属性时遇到问题。以下是 Delphi 代码的相关部分:
uses
mscorlib_TLB, Winapi.ActiveX;
type
// Irrelevant parts of the code omitted
TDotNetObject = class(TObject)
private
FTarget: OleVariant;
FType: _Type;
public
procedure SetProperty(const APropertyName: string; const AValue: OleVariant; const AIndex: Integer = -1);
end;
function VariantToPSafeArray(const AValue: Variant): PSafeArray;
begin
Result := PSafeArray(VarArrayAsPSafeArray(AValue));
end;
procedure TDotNetObject.SetProperty(const APropertyName: string; const AValue: OleVariant; const AIndex: Integer = -1);
var
LPropertyInfo: _PropertyInfo;
LIndex: PSafeArray;
begin
if AIndex >= 0 then
LIndex := VariantToPSafeArray(VarArrayOf([AIndex]))
else
LIndex := nil;
LPropertyInfo := FType.GetProperty(APropertyName, BindingFlags_Instance or BindingFlags_Public or BindingFlags_NonPublic);
if LPropertyInfo <> nil then
LPropertyInfo.SetValue(FTarget, AValue, LIndex);
end;
procedure UpdateDefectStatus(const ADefectID, AStatus: Integer);
var
LObject: TDotNetObject;
begin
// ** Code to obtain the object omitted ***
LObject.SetProperty('Status', AStatus);
end;
mscorlib_TLB 单元来自 Project JEDI 中的 JCL,此处:
https://github.com/project-jedi/jcl/blob/master/jcl/source/windows/mscorlib_TLB.pas
在TDotNetObject.SetProperty中调用LPropertyInfo.SetValue时抛出错误:
Project TestProject.exe raised exception class EOleException with message 'Object of type 'System.Int32' cannot be converted to type 'System.Nullable`1[MTData.Transport.Tracking.DefectReporting.DefectStatus]''.
C# 对象上的 DefectStatus 属性声明为:
public DefectStatus? Status
(即可以为空)
C# 中的 Status 属性类型声明为:
public enum DefectStatus
{
/// <summary>
/// Defect Reported.
/// </summary>
Reported,
/// <summary>
/// Defect assessed.
/// </summary>
Assessed,
/// <summary>
/// Defect on work order.
/// </summary>
OnWorkOrder,
/// <summary>
/// Defect closed.
/// </summary>
Closed
}
我在这里找到了如何使用 C# 处理这种情况的解决方案:
https://stackoverflow.com/a/13270302/3164070
但是我对如何在 Delphi 中做同样的事情有点迷失。有什么想法吗?
编辑
鉴于 Olivier 的回答,我尝试编写一些 Delphi 代码来执行等效操作,如下所示:
procedure InvokeToObject;
var
LType, LDefectStatusType: _Type;
LInvokeFlags: TOleEnum;
LArgs: PSafeArray;
LValue: Integer;
LResult: OleVariant;
LRes: HRESULT;
LResHex: string;
begin
LType := MTDataClr.GetCoreType('System.Enum');
LDefectStatusType := MTDataClr.GetType('MTData.Transport.Tracking.DefectReporting.DefectStatus');
LInvokeFlags := BindingFlags_InvokeMethod or BindingFlags_Static;
LValue := 1;
LArgs := VariantToPSafeArray(VarArrayOf([LDefectStatusType, LValue]));
LRes := LType.InvokeMember_2('ToObject', LInvokeFlags, nil, Null, LArgs, nil, LResult);
LResHex := IntToHex(LRes);
end;
这段代码的目标只是调用 Enum 类型的 ToObject 方法。获取LType和LDefectStatusType成功,但是调用InvokeMember_2没有成功,返回码为:0x80131512,这显然是Missing Member异常。关于我做错了什么有什么想法吗?
最佳答案
问题在于枚举不是 int,这意味着 SetValue()
需要执行双重转换(Int32
到 DefectStatus
> 和 DefectStatus
到 DefectStatus?
),但它不能(它只能执行一个)。
这是一个 C# 代码,可以重现您想要执行的操作:
using System;
using System.Reflection;
public enum DefectStatus
{
Reported,
Assessed,
OnWorkOrder,
Closed
}
public class Defect
{
public DefectStatus? Status {get; set;}
}
public class Test
{
public static void Main()
{
Defect def = new Defect();
PropertyInfo pi = typeof(Defect).GetProperty("Status");
// This throws an ArgumentException
// pi.SetValue(def, 1, null);
// Retrieve the Assessed enum via its numeric value
object assessed = Enum.ToObject(typeof(DefectStatus), 1);
// This works as expected
pi.SetValue(def, assessed, null);
Console.WriteLine(def.Status);
}
}
因此您需要在 Delphi 中检索枚举。为此,您需要使用 API 来访问 Enum
类型并对其调用 ToObject
。
关于c# - 在 .NET 对象上设置 "nullable"属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61334031/
因为对这个问题的第一条评论可能是为什么以及为什么可能使这不是一个重复的问题:我有一个具有三个有效状态的值类型变量(如果重要的话是小数类型):has value | null | unspecified
Nullable 的定义是: [SerializableAttribute] public struct Nullable where T : struct, new() 约束where T : st
以下代码拒绝编译: Nullable!(Nullable!int) nni = Nullable!(Nullable!int)(10); 出现此错误消息: Error: inout method nu
Not Nullable 类型转换为 Nullable 类型的基础知识是什么? CLR 内部发生了什么? 值类型是否在内部转换为引用类型? int i = 100; and int ? i = 7?
我在寻找答案时得到的结果是指从 short 到 int 以及从可空到不可空的转换。但是,我无法理解如何将“较大”类型 int? 转换为“较小”类型 short?。 我能想到的唯一方法就是写一个这样的方
我们在契约(Contract)中使用 Swagger。考虑这个简单的响应 DTO public class Result { public int SomeInt { get;set; } }
有一个Nullable结构,还有另一个静态 Nullable具有三个静态方法的类。 我的问题是,为什么static Nullable中的这些静态方法不能?类进入Nullable结构?将它们定义为两种不
我错误地发现了一些让我吃惊的东西。 我有这个方法 public static string PrintDecimal(decimal? input, string NumberFormat = nul
此代码编译: private static void Main(string[] args) { bool? fred = true; if (fred == true)
希望标题已经很清楚了。 我想看一个更改表语句的示例,该语句可以将 Sybase 表中现有的不可为空的列更改为可以为空。 最佳答案 Modifying the NULL default value of
我使用 C# 8 可空引用类型。 我有一个泛型类,它可能接受可为空引用类型作为类型参数。 有没有办法根据泛型类型参数声明不可为空的类型,这些参数可能是可为空的引用类型(甚至是 Nullable 结构)
考虑以下代码: Nullable dt; dt. dt?. . 如何以及为什么? 最佳答案 因为如果 ?. 左侧的对象为 null,则 null 传播的工作方式永远不会执行右侧的对象。因为您知道右
我一直使用 Nullable<>.HasValue因为我喜欢语义。然而,最近我正在研究其他人现有的代码库,他们在其中使用了 Nullable<> != null。专门代替。 是否有理由使用一个而不是另
我对转换方法“.ToString()”有一个普遍的疑问。起初我使用这个语句进行转换: Nullable SomeProperty; string test = SomeProperty.ToStrin
有没有一种方法可以表达两种相关类型(一种具有可空属性,一种不具有),以便您可以在运行时进行检查后将一种强制转换为另一种? 例如 - type Stat = { count: ?number, }
对于 EF 5.0.0、VS 2012 和 .NET 4.5,当我从现有 SQL Server 2012 数据库添加新的 ADO.NET 实体数据模型时,生成的代码不会区分可空和不可空 varchar
我是 PySpark 的新手,正面临一个奇怪的问题。我试图在加载 CSV 数据集时将某些列设置为不可空。我可以使用非常小的数据集 (test.csv) 重现我的案例: col1,col2,col3 1
假设我有以下类(class): public class GenericClass { public T Find() { //return T if found ot
这个问题在这里已经有了答案: How is the boxing/unboxing behavior of Nullable possible? (3 个答案) 关闭 7 年前。 为什么 null可
Firebase 3.6.0 中的警告。 Xcode 8 - Swift 3。 这些是 Firebase 类:- @class FIROptions @class FIRAuthCredential
我是一名优秀的程序员,十分优秀!