Attribute Value Changed and Get and Set value of the Attribute value with C#
public static bool IsOptionSetChanged(string field, Entity target, Entity preImage)
{
if (target == null)
{
throw new ArgumentNullException(nameof(target));
}
bool result = false;
if (target.Contains(field))
{
int? targetValue = CommonHelper.GetOptionSetValue(field, target);
int? preImageValue = CommonHelper.GetOptionSetValue(field, preImage);
if (!((targetValue == null && preImageValue == null) ||
(targetValue != null && preImageValue != null && targetValue.Value == preImageValue.Value)))
{
result = true;
}
}
return result;
}
-----------------------------------------------------------------------------------------------------------------
public static int? GetOptionSetValue(string key, Entity result)
{
int? value = null;
if (result != null && result.Attributes.Contains(key)
&& result.Attributes[key] != null)
{
OptionSetValue os = null;
if (result.Attributes[key] is AliasedValue)
{
os = (OptionSetValue)((AliasedValue)result.Attributes[key]).Value;
}
else
{
os = (OptionSetValue)result[key];
}
if (os != null)
{
value = Convert.ToInt32(os.Value, CultureInfo.InvariantCulture);
}
}
return value;
}
----------------------------------------------------------------------------------------------------------------
public static void SetAttributeValue(string key, object value, Entity entity)
{
if (entity != null)
{
if (entity.Attributes.Contains(key))
{
entity.Attributes[key] = value;
}
else
{
entity.Attributes.Add(key, value);
}
}
}
{
if (target == null)
{
throw new ArgumentNullException(nameof(target));
}
bool result = false;
if (target.Contains(field))
{
int? targetValue = CommonHelper.GetOptionSetValue(field, target);
int? preImageValue = CommonHelper.GetOptionSetValue(field, preImage);
if (!((targetValue == null && preImageValue == null) ||
(targetValue != null && preImageValue != null && targetValue.Value == preImageValue.Value)))
{
result = true;
}
}
return result;
}
-----------------------------------------------------------------------------------------------------------------
public static int? GetOptionSetValue(string key, Entity result)
{
int? value = null;
if (result != null && result.Attributes.Contains(key)
&& result.Attributes[key] != null)
{
OptionSetValue os = null;
if (result.Attributes[key] is AliasedValue)
{
os = (OptionSetValue)((AliasedValue)result.Attributes[key]).Value;
}
else
{
os = (OptionSetValue)result[key];
}
if (os != null)
{
value = Convert.ToInt32(os.Value, CultureInfo.InvariantCulture);
}
}
return value;
}
----------------------------------------------------------------------------------------------------------------
public static void SetAttributeValue(string key, object value, Entity entity)
{
if (entity != null)
{
if (entity.Attributes.Contains(key))
{
entity.Attributes[key] = value;
}
else
{
entity.Attributes.Add(key, value);
}
}
}
Comments
Post a Comment