I have the code below which I know isn't optimal. I ran the Code Analysis and it gave me the warning
CA1800 : Microsoft.Performance : 'customField', a variable, is cast to type 'DateCustomFieldRef' multiple times in method 'Customer.CustomerToUpdat(SearchResult)'. Cache the result of the 'as' operator or direct cast in order to eliminate the redundant castclass instruction.
and I really don't understand what to do.
CustomFieldRef[] customFields = customer.customFieldList;
for (int f = 3; f < customFields.Length; f++)
{
CustomFieldRef customField = customFields[f];
if (customField is DateCustomFieldRef)
{
DateCustomFieldRef dateField = (DateCustomFieldRef)customField;
if (dateField.internalId != null && dateField.internalId == "created_date")
{
createdDate = dateField.value.ToString();
}
}
if (customField is StringCustomFieldRef)
{
StringCustomFieldRef tradingNameField = (StringCustomFieldRef)customField;
if (businessNameField.internalId != null && businessNameField.internalId == "business_name")
{
businessName = businessNameField.value;
}
}
}
}
Could someone please give me a code example or explain further what it really means?
Thanks in advance.
The problem is in code like:
if (customField is DateCustomFieldRef)
{
DateCustomFieldRef dateField = (DateCustomFieldRef)customField;
These are multiple casts.
Better:
DateCustomFieldRef fieldasDate = customField as DateCustomFieldFRef
if (fieldasDate != null)
{
blablabla using fieldasdate
This avoids the multiple casts.
It means that you're casting (which can be costly) the customField variable multiple times, and that you'd be better of by casting only once.
You can use the as operator to achieve that, since the as operator performs the cast and returns an instance of the desired type, or NULL if the object could not be casted to the desired type.
Like this:
DateCustomFieldRef customField = customFields[f] as DateCustomFieldRef; // the as operator returns null if the casting did not succeed (that is, customFields[f] is not a DatecustomFieldRef instance
if (customField != null)
{
DateCustomFieldRef dateField = customField;
if (dateField.internalId != null && dateField.internalId == "created_date")
{
createdDate = dateField.value.ToString();
}
}
else
{
var stringField = customFields[f] as StringCustomFieldRef;
if (stringField != null )
{
StringCustomFieldRef tradingNameField = stringField;
if (businessNameField.internalId != null && businessNameField.internalId == "business_name")
{
businessName = businessNameField.value;
}
}
}
But, I believe that there probably exists an even better solution (although I do not know your project, nor your code), but wouldn't it be possible to abstract some things away ?
Perhaps you have a CustomField baseclass, and DateCustomFieldRef and StringCustomFieldRef inherit from that Customfield base-class.
If that's the case, you could create a virtual (or maybe even abstract) method in the CustomField base-class, which is overriden in each child-class, which actually returns the value of that field.
Like this:
public class CustomField<T>
{
public string Internalid
{
get;
set;
}
public T Value
{
get;
set;
}
public virtual string GetStringRepresentation()
{
return Value.ToString();
}
}
public class DateCustomField : CustomField<DateTime>
{
public override string GetStringRepresentation()
{
return Value.ToShortDateString();
}
}
Your code can then look much more simple:
foreach( CustomField f in customFields )
{
if( f.InternalId == "created_date" )
{
createdDate = f.GetStringRepresentation();
}
if( f.InternalId == "business_name" )
{
businessName = f.GetStringRepresentation();
}
}
(The code above could be made more simple and clean, but you'll get the drift.)
Related
I'm working on an Angular (front-end) C# (back-end) application. This application uses Entity Framework code first.
So I want to evaluate two models to compare two models and look for their differences. After some investigation, I found that it can be achieved using Reflection. So I have the following code:
public static List<Variance> Compare<T>(this T val1, T val2)
{
var variances = new List<Variance>();
var properties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance);
foreach (var property in properties)
{
var v = new Variance
{
PropertyName = property.Name,
valA = property.GetValue(val1),
valB = property.GetValue(val2)
};
if (v.valA == null && v.valB == null)
{
continue;
}
if (
(v.valA == null && v.valB != null)
||
(v.valA != null && v.valB == null)
)
{
if(v.valA != null)
{
if (v.valA.ToString() == "0" && v.valB == null)
{
continue;
}
}
if(v.valA == null && v.valB != null)
{
continue;
}
variances.Add(v);
continue;
}
if (!v.valA.Equals(v.valB))
{
variances.Add(v);
}
}
return variances;
}
}
public class Variance
{
public string PropertyName { get; set; }
public object valA { get; set; }
public object valB { get; set; }
}
It is working, but only for root model and not their models inside the model. I.E, I'm comparing this model:
public partial class Profile : BaseAuditModel
{
public string FirstName { get; set; }
public string MiddleName { get; set; }
public virtual ICollection<EmploymentHistory> EmploymentHistories { get; set; }
}
The EmploymentHistories detects it has differences, but in reality, they do not have any difference; I think it is marked as a difference because the method does not know what is inside it, so my question is. How can I detect if it is a collection? and when it is a collection iterate and detect the changes as the root one
If you're concerned with child objects ideally defined within the same assembly then you can try something like this to detect those references and use a recursive call:
if (property.PropertyType.IsClass
&& property.PropertyType.Assembly.FullName == typeof(T).Assembly.FullName)
{
var subVariances = Compare(property.GetValue(val1), property.GetValue(val2));
if(subVariances.Any())
variances.AddRange(subVariances);
continue;
}
I cannot imagine this would be particularly efficient. A better approach might be to incorporate a dirty laundry list of changes if you want to do something like tracking changes. If you're doing a straight comparison of two unrelated objects then rather to jumping to a field-by-field inspection, a first pass might involve serializing both object graphs to JSON and doing a string comparison to determine if they are identical before committing to a field-by-field check.
C# 8.0 introduces nullable reference types. Here's a simple class with a nullable property:
public class Foo
{
public String? Bar { get; set; }
}
Is there a way to check a class property uses a nullable reference type via reflection?
In .NET 6, the NullabilityInfoContext APIs were added to handle this. See this answer.
Prior to this, you need to read the attributes yourself. This appears to work, at least on the types I've tested it with.
public static bool IsNullable(PropertyInfo property) =>
IsNullableHelper(property.PropertyType, property.DeclaringType, property.CustomAttributes);
public static bool IsNullable(FieldInfo field) =>
IsNullableHelper(field.FieldType, field.DeclaringType, field.CustomAttributes);
public static bool IsNullable(ParameterInfo parameter) =>
IsNullableHelper(parameter.ParameterType, parameter.Member, parameter.CustomAttributes);
private static bool IsNullableHelper(Type memberType, MemberInfo? declaringType, IEnumerable<CustomAttributeData> customAttributes)
{
if (memberType.IsValueType)
return Nullable.GetUnderlyingType(memberType) != null;
var nullable = customAttributes
.FirstOrDefault(x => x.AttributeType.FullName == "System.Runtime.CompilerServices.NullableAttribute");
if (nullable != null && nullable.ConstructorArguments.Count == 1)
{
var attributeArgument = nullable.ConstructorArguments[0];
if (attributeArgument.ArgumentType == typeof(byte[]))
{
var args = (ReadOnlyCollection<CustomAttributeTypedArgument>)attributeArgument.Value!;
if (args.Count > 0 && args[0].ArgumentType == typeof(byte))
{
return (byte)args[0].Value! == 2;
}
}
else if (attributeArgument.ArgumentType == typeof(byte))
{
return (byte)attributeArgument.Value! == 2;
}
}
for (var type = declaringType; type != null; type = type.DeclaringType)
{
var context = type.CustomAttributes
.FirstOrDefault(x => x.AttributeType.FullName == "System.Runtime.CompilerServices.NullableContextAttribute");
if (context != null &&
context.ConstructorArguments.Count == 1 &&
context.ConstructorArguments[0].ArgumentType == typeof(byte))
{
return (byte)context.ConstructorArguments[0].Value! == 2;
}
}
// Couldn't find a suitable attribute
return false;
}
See this document for details.
The general gist is that either the property itself can have a [Nullable] attribute on it, or if it doesn't the enclosing type might have [NullableContext] attribute. We first look for [Nullable], then if we don't find it we look for [NullableContext] on the enclosing type.
The compiler might embed the attributes into the assembly, and since we might be looking at a type from a different assembly, we need to do a reflection-only load.
[Nullable] might be instantiated with an array, if the property is generic. In this case, the first element represents the actual property (and further elements represent generic arguments). [NullableContext] is always instantiated with a single byte.
A value of 2 means "nullable". 1 means "not nullable", and 0 means "oblivious".
.NET 6 Preview 7 adds reflection APIs to get nullability info.
Libraries: Reflection APIs for nullability information
Obviously, this only helps folks targeting .NET 6+.
Getting top-level nullability information
Imagine you’re implementing a serializer. Using these new APIs the serializer can check whether a given property can be set to null:
private NullabilityInfoContext _nullabilityContext = new NullabilityInfoContext();
private void DeserializePropertyValue(PropertyInfo p, object instance, object? value)
{
if (value is null)
{
var nullabilityInfo = _nullabilityContext.Create(p);
if (nullabilityInfo.WriteState is not NullabilityState.Nullable)
{
throw new MySerializerException($"Property '{p.GetType().Name}.{p.Name}'' cannot be set to null.");
}
}
p.SetValue(instance, value);
}
I wrote a library to do reflection of NRT types - internally it looks at the generated attributes and gives you a simple API:
https://github.com/RicoSuter/Namotion.Reflection
Late answer.
This is what I ended up using thanks to Bill Menees:
static bool IsMarkedAsNullable(PropertyInfo p)
{
return new NullabilityInfoContext().Create(p).WriteState is NullabilityState.Nullable;
}
// Tests:
class Foo
{
public int Int1 { get; set; }
public int? Int2 { get; set; } = null;
public string Str1 { get; set; } = "";
public string? Str2 { get; set; } = null;
public List<Foo> Lst1 { get; set; } = new();
public List<Foo>? Lst2 { get; set; } = null;
public Dictionary<int, object> Dic1 { get; set; } = new();
public Dictionary<int, object>? Dic2 { get; set; } = null;
}
....
var props = typeof(Foo).GetProperties();
foreach(var prop in props)
{
Console.WriteLine($"Prop:{prop.Name} IsNullable:{IsMarkedAsNullable(prop)}");
}
// outputs:
Prop:Int1 IsNullable:False
Prop:Int2 IsNullable:True
Prop:Str1 IsNullable:False
Prop:Str2 IsNullable:True
Prop:Lst1 IsNullable:False
Prop:Lst2 IsNullable:True
Prop:Dic1 IsNullable:False
Prop:Dic2 IsNullable:True
A great answer there by #rico-suter !
The following is for those who just want a quick cut-and-paste solution until the real McCoy is available (see the proposal https://github.com/dotnet/runtime/issues/29723 ).
I put this together based on #canton7's post above plus a short look at the ideas in #rico-suter's code. The change from the #canton7's code is just abstracting the list of attribute sources and including a few new ones.
private static bool IsAttributedAsNonNullable(this PropertyInfo propertyInfo)
{
return IsAttributedAsNonNullable(
new dynamic?[] { propertyInfo },
new dynamic?[] { propertyInfo.DeclaringType, propertyInfo.DeclaringType?.DeclaringType, propertyInfo.DeclaringType?.GetTypeInfo() }
);
}
private static bool IsAttributedAsNonNullable(this ParameterInfo parameterInfo)
{
return IsAttributedAsNonNullable(
new dynamic?[] { parameterInfo },
new dynamic?[] { parameterInfo.Member, parameterInfo.Member.DeclaringType, parameterInfo.Member.DeclaringType?.DeclaringType, parameterInfo.Member.DeclaringType?.GetTypeInfo()
);
}
private static bool IsAttributedAsNonNullable( dynamic?[] nullableAttributeSources, dynamic?[] nullableContextAttributeSources)
{
foreach (dynamic? nullableAttributeSource in nullableAttributeSources) {
if (nullableAttributeSource == null) { continue; }
CustomAttributeData? nullableAttribute = ((IEnumerable<CustomAttributeData>)nullableAttributeSource.CustomAttributes).FirstOrDefault(x => x.AttributeType.FullName == "System.Runtime.CompilerServices.NullableAttribute");
if (nullableAttribute != null && nullableAttribute.ConstructorArguments.Count == 1) {
CustomAttributeTypedArgument attributeArgument = nullableAttribute.ConstructorArguments[0];
if (attributeArgument.ArgumentType == typeof(byte[])) {
var args = (ReadOnlyCollection<CustomAttributeTypedArgument>)(attributeArgument.Value ?? throw new NullReferenceException("Logic error!"));
if (args.Count > 0 && args[0].ArgumentType == typeof(byte)) {
byte value = (byte)(args[0].Value ?? throw new NullabilityLogicException());
return value == 1; // 0 = oblivious, 1 = nonnullable, 2 = nullable
}
} else if (attributeArgument.ArgumentType == typeof(byte)) {
byte value = (byte)(attributeArgument.Value ?? throw new NullReferenceException("Logic error!"));
return value == 1; // 0 = oblivious, 1 = nonnullable, 2 = nullable
} else {
throw new InvalidOperationException($"Unrecognized argument type for NullableAttribute.");
}
}
}
foreach (dynamic? nullableContextAttributeSource in nullableContextAttributeSources) {
if (nullableContextAttributeSource == null) { continue; }
CustomAttributeData? nullableContextAttribute = ((IEnumerable<CustomAttributeData>)nullableContextAttributeSource.CustomAttributes).FirstOrDefault(x => x.AttributeType.FullName == "System.Runtime.CompilerServices.NullableContextAttribute");
if (nullableContextAttribute != null && nullableContextAttribute.ConstructorArguments.Count == 1) {
CustomAttributeTypedArgument attributeArgument = nullableContextAttribute.ConstructorArguments[0];
if (attributeArgument.ArgumentType == typeof(byte)) {
byte value = (byte)(nullableContextAttribute.ConstructorArguments[0].Value ?? throw new NullabilityLogicException());
return value == 1;
} else {
throw new InvalidOperationException($"Unrecognized argument type for NullableContextAttribute.");
}
}
}
return false;
}
It's only the string? which gets a bit tricky. The rest of the nullable types are pretty straightforward to find out. For strings I used the following method, which you need to pass in a PropertyInfo object taken via reflection.
private bool IsNullable(PropertyInfo prop)
{
return prop.CustomAttributes.Any(x => x.AttributeType.Name == "NullableAttribute");
}
Good Morning All,
I'm trying to use "Contains" to see if an object is within the collection. When I break I can see that the object is indeed part of the collection however "Contains" seems to be returning false indicating the item is not in the collection. Any idea what I'm doing wrong?
if(HttpContext.Current.Session["AutoPayTypes"] != null)
{
var autopays = HttpContext.Current.Session["AutoPayTypes"] as List<PaymentTypeInfo>;
char? coverageProductLine = null;
if(entityProps.ContainsKey("CoverageProductLine"))
{
coverageProductLine = (char?)entityProps["CoverageProductLine"];
}
var paymentTypeInfoRepository = new PaymentTypeInfoRepository();
var payType = paymentTypeInfoRepository.GetPaymentTypeInfo(paymentAdd.PayType,
coverageProductLine);
if (autopays != null && payType != null)
paymentAdd.DaysPaid = autopays.Contains(payType) ? null : paymentAdd.DaysPaid;
}
If the object is not in the collection the "DaysPaid" needs to be null. Any ideas?
***UPDATE
PaymentTypeInfo is a standard LinqToSql generated class. Equals nor GetHashCode has been overridden at this point. Here is it's source.
[Table(Name="dbo.S_OptPaymentType")]
public partial class PaymentTypeInfo
{
private string _PaymentId;
private string _PaymentCode;
private System.Nullable<char> _CoverageType;
private string _ActionCode;
private System.Nullable<char> _PaymentType;
private string _BenAction;
private System.Nullable<char> _BenPremDisFlag;
private string _APNextToLastAct;
private string _APLastAct;
public PaymentTypeInfo()
{
}
[Column(Storage="_PaymentId", DbType="Char(3) NOT NULL", CanBeNull=false)]
public string PaymentId
{
get
{
return this._PaymentId;
}
set
{
if ((this._PaymentId != value))
{
this._PaymentId = value;
}
}
}
[Column(Storage="_PaymentCode", DbType="Char(2) NOT NULL", CanBeNull=false)]
public string PaymentCode
{
get
{
return this._PaymentCode;
}
set
{
if ((this._PaymentCode != value))
{
this._PaymentCode = value;
}
}
}
[Column(Storage="_CoverageType", DbType="Char(1)")]
public System.Nullable<char> CoverageType
{
get
{
return this._CoverageType;
}
set
{
if ((this._CoverageType != value))
{
this._CoverageType = value;
}
}
}
[Column(Storage="_ActionCode", DbType="VarChar(3)")]
public string ActionCode
{
get
{
return this._ActionCode;
}
set
{
if ((this._ActionCode != value))
{
this._ActionCode = value;
}
}
}
[Column(Name="PaymentType", Storage="_PaymentType", DbType="Char(1)")]
public System.Nullable<char> PaymentType
{
get
{
return this._PaymentType;
}
set
{
if ((this._PaymentType != value))
{
this._PaymentType = value;
}
}
}
[Column(Storage="_BenAction", DbType="VarChar(3)")]
public string BenAction
{
get
{
return this._BenAction;
}
set
{
if ((this._BenAction != value))
{
this._BenAction = value;
}
}
}
[Column(Storage="_BenPremDisFlag", DbType="Char(1)")]
public System.Nullable<char> BenPremDisFlag
{
get
{
return this._BenPremDisFlag;
}
set
{
if ((this._BenPremDisFlag != value))
{
this._BenPremDisFlag = value;
}
}
}
[Column(Storage="_APNextToLastAct", DbType="VarChar(3)")]
public string APNextToLastAct
{
get
{
return this._APNextToLastAct;
}
set
{
if ((this._APNextToLastAct != value))
{
this._APNextToLastAct = value;
}
}
}
[Column(Storage="_APLastAct", DbType="VarChar(3)")]
public string APLastAct
{
get
{
return this._APLastAct;
}
set
{
if ((this._APLastAct != value))
{
this._APLastAct = value;
}
}
}
}
Thanks,
~ck in San Diego
EDIT: As Ahmad pointed out, your conditional operator usage is incorrect. However, you don't even need to use the conditional operator here, as one of the branches results in a no-op. Just use this:
if (autopays != null && payType != null && !autopays.Contains(payType))
{
paymentAdd.DaysPaid = null;
}
Original answer
You haven't shown any thing about PaymentTypeInfo - does it override Equals and GetHashCode appropriately? If not, the containment check will be performed using reference identity, and it's very unlikely that the reference in the session is the same as the reference in the repository.
Either make PaymentTypeInfo override Equals and GetHashCode, or pass an appropriate IEqualityComparer<PaymentTypeInfo> into the Contains method.
(As SLaks mentions in the comments, in this case GetHashCode won't actually get called - but you should always override both Equals and GetHashCode or neither of them; if you do override them, you should do so in a consistent manner.)
Unless payType overrides Equals or you specify an IEqualityComparer, Contains will compare by reference.
Your collection probably contains a different instance of the class which is logically equivalent.
It's possible you're running into an equality issue - Contains() uses the IEquatable.Equals method, so you might check to make sure that that's going to return true for separate instances of the PaymentTypeInfo class.
Every post so far has a valid point; depending on the type being used Contains may not suffice. I am addressing a different part of your question though:
If the object is not in the collection
the "DaysPaid" needs to be null. Any
ideas?
How about switching the order of your ternary operator values to match the above statement? Use this:
paymentAdd.DaysPaid = autopays.Contains(payType) ? paymentAdd.DaysPaid : null;
Instead of this:
paymentAdd.DaysPaid = autopays.Contains(payType) ? null : paymentAdd.DaysPaid;
If the statement evaluates to false the 2nd item will be used, so make it null. The structure is:
logic statement ? true : false
Can you post the source of the PaymentType class? I am fairly certain that this type does not provided value-based semantics so the Contains method is forced to resort to using identity equality (which is not giving you the results you want).
If this is the case you may be interested in these articles I wrote on this topic:
All types are not compared equally
All types are not compared equally (part 2)
How does the == operator really function in C#? If it used to compare objects of class A, will it try to match all of A's properties, or will it look for pointers to the same memory location (or maybe something else)?
Let's create a hypothetical example. I'm writing an application that utilizes the Twitter API, and it has a Tweet class, which has all the properties of a single tweet: text, sender, date&time, source, etc. If I want to compare objects of class Tweet for equivalence, can I just use:
Tweet a, b;
if (a == b)
{
//do something...
}
Will that check for equivalence of all the properties of the Tweet class between a and b?
If not, would the correct approach be to overload the == operator to explicitly check for equivalence of all the fields?
UPDATE: From the first two answers, am I right in assuming:
If the == operator or Equals method is not overloaded for a class, the == operator for the object class is used.
The == operator for the object class checks for equality in memory location.
I have to overload the == operator or the Equals method to accomplish this task.
In the overload, I have to check for equivalence in properties manually, so there is no way to do it semi-automatically, say, in a loop, right?
UPDATE #2: Yuriy made a comment that it is possible to do check for equivalence in properties in the == operator with reflection. How can this be done? Could you give me some sample code? Thanks!
For reference types, the default implementations of both the == operator and the Equals() method will simply check that both objects have the same reference, and are therefore the same instance.
If you want to check the contents of two different objects are equal then you must write the code to do it yourself, one way or another. It would be possible to do with reflection (the MbUnit test framework does something along these lines) but with a heavy performance penalty and a good chance that it wouldn't do quite what you expected anyway, and you should implement == or Equals and GetHashCode by hand.
MSDN has a good example of how to do it:
public override bool Equals(object o)
{
try
{
return (bool) (this == (DBBool) o);
}
catch
{
return false;
}
}
Then you overload the == and !=:
// Equality operator. Returns dbNull if either operand is dbNull,
// otherwise returns dbTrue or dbFalse:
public static DBBool operator ==(DBBool x, DBBool y)
{
if (x.value == 0 || y.value == 0) return dbNull;
return x.value == y.value? dbTrue: dbFalse;
}
// Inequality operator. Returns dbNull if either operand is
// dbNull, otherwise returns dbTrue or dbFalse:
public static DBBool operator !=(DBBool x, DBBool y)
{
if (x.value == 0 || y.value == 0) return dbNull;
return x.value != y.value? dbTrue: dbFalse;
}
And don't forget to overload the GetHash method.
Edit:
I wrote the following quick sample for using reflection in a compare. This would have to be much more comprehensive, I might try doing a blog on it if people want me to:
public class TestEquals
{
private int _x;
public TestEquals(int x)
{
this._x = x;
}
public override bool Equals(object obj)
{
TestEquals te = (TestEquals)obj;
if (te == null) return false;
foreach (var field in typeof(TestEquals)
.GetFields(BindingFlags.NonPublic | BindingFlags.Instance))
{
if (!field.GetValue(this).Equals(field.GetValue(te)))
return false;
}
return true;
}
}
The proper approach is the overload the equals method of the Tweet class in addition to the == operator, as described here.
Will that check for equivalence of all the properties of the Tweet class between a and b?
No
If not, would the correct approach be to overload the == operator to explicitly check for equivalence of all the fields?
You can either overload the == operator, or overload the Equals function.
Edit
#Yuriy gave a good example for compating all the non public variables. Since i also wrote an example, here it is (mine compares properties)
class TwitterItem
{
private string myValue = "default value";
public string Value1
{
get { return myValue; }
set { myValue = value; }
}
public string Value2
{
get { return myValue; }
set { myValue = value; }
}
public string Value3
{
get { return myValue; }
set { myValue = value; }
}
public override bool Equals(object obj)
{
if (base.Equals(obj)) return true;
Type type = typeof(TwitterItem);
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
if (false == property.GetValue(this, null).Equals(property.GetValue(obj, null)))
return false;
}
return true;
}
}
You can compare the properties using reflection:
var a = new Entity() { Name = "test", ID = "1" };
var b = new Entity() { Name = "test", ID = "1" };
var c = new Entity() { Name = "test", ID = "2" };
System.Diagnostics.Debug.WriteLine(a.Equals(b));//Returns true
System.Diagnostics.Debug.WriteLine(a.Equals(c));//Returns false
public class Entity
{
public string Name { get; set; }
public string ID { get; set; }
public override bool Equals(object obj)
{
var t = obj.GetType();
foreach (var p in t.GetProperties())
{
if (t.GetProperty(p.Name).GetValue(obj, null) != t.GetProperty(p.Name).GetValue(this, null))
return false;
}
return true;
}
}
I'm working through Josh Smith's CommandSink code obviously do not understand something about the "as" keyword in C#.
I don't understand why he wrote the line:
IsValid = _fe != null || _fce != null;
since he only needed to write:
IsValid = depObj != null;
Since it would never be the case the _fe would be null and _fce not null, or visa versa, right? Or am I missing something about how "as" casts variables?
class CommonElement
{
readonly FrameworkElement _fe;
readonly FrameworkContentElement _fce;
public readonly bool IsValid;
public CommonElement(DependencyObject depObj)
{
_fe = depObj as FrameworkElement;
_fce = depObj as FrameworkContentElement;
IsValid = _fe != null || _fce != null;
}
...
ANSWER:
The answer is what Marc said in his comment "that is the whole point of "as" - it won't throw an exception - it just reports null."
and here is the proof:
using System;
namespace TestAs234
{
class Program
{
static void Main(string[] args)
{
Customer customer = new Customer();
Employee employee = new Employee();
Person.Test(customer);
Person.Test(employee);
Console.ReadLine();
}
}
class Person
{
public static void Test(object obj)
{
Person person = obj as Customer;
if (person == null)
{
Console.WriteLine("person is null");
}
else
{
Console.WriteLine("person is of type {0}", obj.GetType());
}
}
}
class Customer : Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
class Employee : Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
}
}
as will return an object of the type you requested, if the operand is compatible. If it isn't, it will return null. If you use as and it is possible that the cast will fail, you need to check to make sure the reference is valid.
For example, if depObj was of type String, it would not be null, but it would also not be able to be converted to either of the requested types and both of those variables would become null.
as does "cast, if it is", and equivalent to:
(X is TYPE) ? (TYPE) X : null
it is however, more efficient than is + cast.
depObj may implement either interface, none, or both.
IsValid = _fe != null || _fce != null;
and
IsValid = depObj != null;
are not the same tests because if depObj is not of type FrameworkElement nor FrameworkContentElement but is not null the second test will return true, while the first will return false.
What if depObj is neither a FrameworkElement or a FrameworkContentElement? I don't know the full scenario (i.e. what the types are likely to be), but this seems a reasonable defensive strategy.
First, the as keyword includes a is check.
if( o is A)
a = (A) o;
is the same as
a = o as A;
Second, as does not convert the Type like a cast does, even if a conversion operator from Type A to B is defined.
What if the DependencyObject depObj was actually a FrameworkOtherTypeOfElement
Then depObj would not be null
but the attempted as Casts would both evaluate to null and _fe & _fce would both be null
as is equivalent to doing
if(I Can Cast This Object)
//Then cast it
else
//Return null