我正在运行一个C#代码,用于比较CRM中的预先和后置图像以确定字段是否更改(长话短说:外部进程在每次更新记录时更新每个字段,即使字段没有更改,我无法控制)。 我想使用CRM GetAttributeValue(attributeName)来完成此操作,但我希望在不知道字段名称的情况下动态执行它。 因此,例如,我想要这样做:
// pretend the value of firstname is not hard-coded but submitted on a form
// (not really on a form, but just know it's not hard-coded like it is below.)
string fieldToCheck = "firstname";
if (preImage.GetAttributeValue<T>(fieldToCheck) != postImage.GetAttributeValue<T>(fieldToCheck))
{
// do something here. I've tried something like the below, but it doesn't work with error "t is a variable but used like a type".
Type t = preImage.Attributes[fieldToCheck].GetType();
var val = preImage.GetAttributeValue<t>(fieldToCheck);
}
我遇到的问题是,
<T>
取决于fieldToCheck
的值而有所不同。在firstname的情况下,它将是<string>
;在new_DateOpened的情况下,它将是<DateTime>
等等。我应该可以想出如何动态获取T的值,但我可能脑抽了。