Default value of DateTime is not working as expected - c#

I have a class with DateTime and DateTime? variables,"if (value != defaultValue)" should fail and the control should not go inside. but then here in my case it is executing.
(value != defaultValue) is not working. what can I replace the code with.
using System;
using System.Collections.Generic;
using System.Reflection;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
DateClass v = new DateClass();
v.Date1 = default;
v.Date1 = default;
foreach (PropertyInfo pi in v.GetType().GetProperties())
{
var type = pi.PropertyType;
var value = pi.GetValue(v);
var defaultValue = GetDefault(type);
if (value != defaultValue)
{
Console.WriteLine("not equal");
}
else
{
Console.WriteLine("equal");
}
}
}
public static object GetDefault(Type type)
{
if (type.IsValueType)
{
var k = Activator.CreateInstance(type);
return k;
}
return null;
}
}
public class DateClass
{
public DateTime Date1
{
get; set;
}
public DateTime? Date2
{
get; set;
}
}
}

The problem is that the compile-time type of PropertyInfo.GetValue is object... which means that basically you're boxing the DateTime, and then comparing two different references, with a reference comparison.
Personally I'd say that a field with a value of DateTime.MinValue (aka default(DateTime) is still non-null (as is an int field with a value of 0, for example), and that you should either change the name of the method or only exclude genuine null values. However, if you do want to down this route, I'd change your condition to just
if (!Equals(value, defaultValue))
That will perform a value equality check, which is what you want.

Related

Dealing with derived properties with same name as in base class using reflection

I will try to explain my best, but it is a tricky one to explain.
I am having a problem using reflection when a derived object redefines a property already in a base class.
Let's consider the following classes to start with:
// The base class
namespace MyNamesapce
{
public abstract class MyClassBase: IMyClassBase
{
[JsonConstructor]
public MyClassBase()
{
}
public string Info { get; set; }
public string Unit { get; set; }
public string Name { get; set; }
}
}
// MyClassArray is most of the time used
namespace MyNamesapce
{
public class MyClassArray<TType> : MyClassBase, IMyClassArray<TType>
{
public MyClassArray()
{
}
[JsonConstructor]
public MyClassArray(IEnumerable<TType> value, TType minValue, TType maxValue)
{
MinValue = minValue;
MaxValue = maxValue;
Value = value;
}
public IEnumerable<TType> Value { get; set; }
public TType MinValue { get; set; }
public TType MaxValue { get; set; }
}
}
// In some rare cases we need 2D arrays
namespace MyNamesapce
{
public class MyClass2DArray<TType> : MyClassArray<TType>, IMyClass2DArray<TType>
{
private int[] _arraySize { get; set; }
public MyClass2DArray()
{
}
[JsonConstructor]
public MyClass2DArray(TType[][] value, TType minValue, TType maxValue)
{
MinValue = minValue;
MaxValue = maxValue;
_arraySize = new int[2] { value.Count(), value[0].Length };
Value = value;
}
public new TType[][] Value
{
get
{
TType[][] array2D = new TType[_arraySize[0]][];
// Reconstruct the 2D array
TType[] tmpArray;
int startIdx = 0;
for (int numArrays = 0; numArrays < _arraySize[0]; numArrays++)
{
tmpArray = new TType[_arraySize[1]];
Array.Copy(base.Value.ToArray(), startIdx, tmpArray, 0, _arraySize[1]);
startIdx += _arraySize[1];
array2D[numArrays] = tmpArray;
}
return array2D;
}
set
{
// Should not be able to set _value to null
if (value == null)
return;
base.Value = value.SelectMany(v => v).ToArray();
}
}
}
}
I now need to get all the properties from all instances of MyClassArray and MyClassArray2D. You will say, there are plenty of threads discussing that very point, just use "GetType().GetProperties()" for the former and use "GetType().GetProperty(..., BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly)" for the latter.
The problem is that I do not know in advance which class is being processed. In my system when deserialising a Json, instances of both MyClassArray and MyClassArray2D have to be reconstructed, which is done using the following setter:
public static void SetProperty(this Object obj, string propName, Object value)
{
PropertyInfo info = null;
object[] indexer = null;
string[] nameParts = propName.Split('.');
if (obj == null) { return; }
var props = obj.GetType().GetProperties();
for (int idx = 0; idx < nameParts.Count() - 1; idx++)
{
try
{
indexer = null;
// Try to access normal property
info = obj.GetType().GetProperty(nameParts[idx]);
if (info == null)
continue;
obj = info.GetValue(obj, indexer);
}
catch
{
info = null;
indexer = null;
}
}
if (obj != null)
{
// !!! Note that here, using declare only will only work when using derived classes
PropertyInfo propertyToSet = obj.GetType().GetProperty(nameParts.Last(), BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly); // | BindingFlags.DeclaredOnly);
propertyToSet?.SetValue(obj, value);
}
else
{
throw new SystemException($"Could not find the property {propName}");
}
}
As you can see an object is passed in to SetProperty() (that can be of any type).
When it is of type MyClassArray, there are no problems, but if it is of type MyClassArray2D it does not quite work as the latter redefines "Value", which will break the logic as 2 properties called value will exist. I need a way to detect that.
The first loop seems to do the right thing. "obj = info.GetValue(obj, indexer);" will return "obj" containing all the versions of "Value". The problem is in the next part of SetProperty().
How can I detect when more than one "Value" property is in "obj"? And how to always pick the derived version of "Value"?
Also if I just use "BindingFlags.DeclaredOnly" as done here in my code snipet, properties from the base class get lost/disappear, which is undesirable.
Is there maybe a way to return in "obj" all the properties without the duplicates coming from the base class? Or some kind of property filter maybe?

SQLCLR custom aggregate with multiple sql_variant parameters

Hy,
I have post a question about CLR User-Defined Aggregates few month ago oon this post.
This works like a charm. But now I would like to quite the same functions with the two parameters in sql_variant type.
Like in my previous post, the two function would be sMax and sMin and will return the first value depending on the second.
I found that the sql_variant type is a object type in C#. But I having difficulties to accumulate and compare the object.
What is the best option to compare this two object without knowing the type?
When using SQL_VARIANT / object, you can determine the type by using the GetType() method as follows:
[Microsoft.SqlServer.Server.SqlFunction(IsDeterministic = false, IsPrecise = true)]
public static SqlBoolean GetIfSmallInt(object SomeValue)
{
return (SomeValue.GetType() == typeof(SqlInt16));
}
And test it using:
DECLARE #DateTime DATETIME = GETDATE();
SELECT dbo.GetIfSmallInt(#DateTime);
-- 0
DECLARE #SmallInt SMALLINT = 5;
SELECT dbo.GetIfSmallInt(#SmallInt);
-- 1
Please keep in mind that using SQL_VARIANT / object has a noticeable performance penalty. Only use it when you absolutely need to. If you just need to pass in INT / SMALLINT / BIGINT, then use BIGINT / SqlInt64 as the input parameter type.
Thanks for your response. I use this philosophy to complete my aggregate function.
So far, it's working but not everythinks fine...
the isNull not working
without the isNull, using the Convert.ToString is not good, it replace null value by empty string. But without it crash with empty values
in the Read function : use the ReadString function. ReadBytes is better?
To perform the CompareTo, using the Convert and the toString, is it the good approach ?
The code :
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Diagnostics.Eventing.Reader;
using System.Globalization;
using Microsoft.SqlServer.Server;
using System.Text;
using System.Collections;
using System.IO;
[Serializable]
[SqlUserDefinedAggregate(
Format.UserDefined,
IsInvariantToOrder = true,
IsInvariantToNulls = true,
IsInvariantToDuplicates = true,
MaxByteSize = -1)]
public struct sMax : IBinarySerialize, INullable
{
#region Helpers
private struct MyData
{
public object Data { get; set; }
public InType DataType { get; set; }
public object Group { get; set; }
public InType GroupType { get; set; }
public int CompareTo(MyData other)
{
if (Group == null)
return other.Group == null ? 0 : -1;
if (other.Group == null)
return 1;
if (GroupType == InType.Int)
return Convert.ToInt32(Group).CompareTo(Convert.ToInt32(other.Group));
if (GroupType == InType.BigInt)
return Convert.ToInt64(Group).CompareTo(Convert.ToInt64(other.Group));
if (GroupType == InType.Double)
return Convert.ToDouble(Group).CompareTo(Convert.ToDouble(other.Group));
if (GroupType == InType.Date)
return Convert.ToDateTime(Group.ToString()).CompareTo(Convert.ToDateTime(other.Group.ToString()));
if (GroupType == InType.String)
return Convert.ToString(Group).CompareTo(Convert.ToString(other.Group));
else
return 0;
}
public static bool operator < (MyData left, MyData right)
{
return left.CompareTo(right) == -1;
}
public static bool operator > (MyData left, MyData right)
{
return left.CompareTo(right) == 1;
}
}
private enum InType
{
String,
Int,
BigInt,
Date,
Double,
Unknow
}
private InType GetType(object value)
{
if (value.GetType() == typeof(SqlInt32))
return InType.Int;
else if (value.GetType() == typeof(SqlInt64))
return InType.BigInt;
else if (value.GetType() == typeof(SqlString))
return InType.String;
else if (value.GetType() == typeof(SqlDateTime))
return InType.Date;
else if (value.GetType() == typeof(SqlDouble))
return InType.Double;
else
return InType.Unknow;
}
#endregion
private MyData _maxItem;
public void Init()
{
_maxItem = default(MyData);
this.IsNull = true;
}
public void Accumulate(object data, object group)
{
if (data != null && group != null)
{
var current = new MyData
{
Data = data,
Group = group,
DataType = GetType(data),
GroupType = GetType(group)
};
if (current > _maxItem)
{
_maxItem = current;
}
}
}
public void Merge(sMax other)
{
if (other._maxItem > _maxItem)
{
_maxItem = other._maxItem;
}
}
public SqlString Terminate()
{
return this.IsNull ? SqlString.Null : new SqlString(_maxItem.Data.ToString());
}
public void Read(BinaryReader reader)
{
IsNull = reader.ReadBoolean();
_maxItem.Group = reader.ReadString();
_maxItem.Data = reader.ReadString();
if (_maxItem.Data != null)
this.IsNull = false;
}
public void Write(BinaryWriter writer)
{
writer.Write(this.IsNull);
writer.Write(_maxItem.Group.ToString());
writer.Write(_maxItem.Data.ToString());
}
public Boolean IsNull { get; private set; }
}

Using SetValue with implicit conversion

Essentially, I'm trying to use
field.SetValue(obj, val);
Where val's type can be implicitly converted to the true field type, but isn't 'directly' assignable. Of course, I get the usual ArgumentException: Object type cannot be converted to target type. Is there a way to do this without manually finding and calling the constructor?
Try with Convert.ChangeType:
field.SetValue(obj, Convert.ChangeType(val, field.PropertyType), null);
Check the following Setting a property by reflection with a string value for further info.
This is a quite complicated question, and I don't know a way to make it work in a sigle line. While the Convert.ChangeType works for simple cases, it will fail in the following cases :
The target type is a Nullable enum, and you use an integer value (not an enum value) : in that case, you'l need use Enum.ToObject to make it work.
Your value is DBNull.Value : You will need to test it and assign null in that case
The target type is not the same type of number as the value you want to set : Convert.ChangeType will help you here.
Here's a sample that illustrates how to do it :
public void SetFieldValue(FieldInfo field, object targetObj, object value)
{
object valueToSet;
if (value == null || value == DBNull.Value)
{
valueToSet = null;
}
else
{
Type fieldType = field.FieldType;
//assign enum
if (fieldType.IsEnum)
valueToSet = Enum.ToObject(fieldType, value);
//support for nullable enum types
else if (fieldType.IsValueType && IsNullableType(fieldType))
{
Type underlyingType = Nullable.GetUnderlyingType(fieldType);
valueToSet = underlyingType.IsEnum ? Enum.ToObject(underlyingType, value) : value;
}
else
{
//we always need ChangeType, it will convert the value to the proper number type, for example.
valueToSet = Convert.ChangeType(value, fieldType);
}
}
field.SetValue(targetObj, valueToSet);
}
A unit test for the function :
enum TestEnum
{
DummyValue
}
class TestClass
{
public int IntValue;
public decimal DecimalValue;
public int? NullableInt;
public TestEnum EnumValue;
public TestEnum? NullableEnumValue;
public TestClass ObjectValue;
}
[TestFixture]
public class DataObjectBinderFixture
{
private TestClass _testObject;
private void SetFieldValue(string fieldName, object value)
{
var fieldInfo = typeof (TestClass).GetField(fieldName);
ReflectionUtils.SetFieldValue(fieldInfo, _testObject, value);
}
[Test]
public void TestSetValue()
{
_testObject = new TestClass();
SetFieldValue("IntValue", 2.19);
SetFieldValue("IntValue", DBNull.Value);
SetFieldValue("DecimalValue", 1);
SetFieldValue("NullableInt", null);
SetFieldValue("NullableInt", 12);
SetFieldValue("EnumValue", TestEnum.DummyValue);
SetFieldValue("EnumValue", 0);
SetFieldValue("NullableEnumValue", TestEnum.DummyValue);
SetFieldValue("NullableEnumValue", null);
SetFieldValue("NullableEnumValue", 0);
SetFieldValue("NullableEnumValue", DBNull.Value);
SetFieldValue("ObjectValue", DBNull.Value);
}
}

How can I create a generic method to return a specific type specified by the call?

The following code gives me the errors:
Cannot implicitly convert type T to string.
Cannot implicitly convert type T to int.
What do I have to do to get this method to return the type of variable I define with T when I call it?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestGener234
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("his first name is {0}", GetPropertyValue<string>("firstName"));
Console.WriteLine("his age is {0}", GetPropertyValue<int>("age"));
}
public static T GetPropertyValue<T>(string propertyIdCode)
{
if (propertyIdCode == "firstName")
return "Jim";
if (propertyIdCode == "age")
return 32;
return null;
}
}
}
Addendum:
Here is a more complete example of why I needed the generic solution, i.e. I have a class that saves its values as strings no matter what the type, and this generic solution simply makes the calling code cleaner:
using System;
using System.Collections.Generic;
namespace TestGener234
{
class Program
{
static void Main(string[] args)
{
List<Item> items = Item.GetItems();
foreach (var item in items)
{
string firstName = item.GetPropertyValue<string>("firstName");
int age = item.GetPropertyValue<int>("age");
Console.WriteLine("First name is {0} and age is {1}.", firstName, age);
}
Console.ReadLine();
}
}
public class Item
{
public string FirstName { get; set; }
public string Age { get; set; }
public static List<Item> GetItems()
{
List<Item> items = new List<Item>();
items.Add(new Item { FirstName = "Jim", Age = "34" });
items.Add(new Item { FirstName = "Angie", Age = "32" });
return items;
}
public T GetPropertyValue<T>(string propertyIdCode)
{
if (propertyIdCode == "firstName")
return (T)(object)FirstName;
if (propertyIdCode == "age")
return (T)(object)(Convert.ToInt32(Age));
return default(T);
}
}
}
That is troublesome; to make the compiler happy you can double-cast, but that implies a box for value types:
public static T GetPropertyValue<T>(string propertyIdCode)
{
if (propertyIdCode == "firstName")
return (T)(object)"Jim";
if (propertyIdCode == "age")
return (T)(object)32;
return default(T);
}
In reality, I think you may be better just using an object return type.
This is an abuse of generics. If you have a small number of types that the generic type parameter could possibly be then just replace it with that many methods:
string GetTextProperty(string propertyName) { ... }
int GetNumberProperty(string propertyName) { ... }
Giraffe GetGiraffeProperty(string propertyName) { ... }
This should work...
public static T GetPropertyValue<T>(string propertyIdCode)
{
if (propertyIdCode == "firstName")
return (T)Convert.ChangeType("Jim", typeof(T));
if (propertyIdCode == "age")
return (T)Convert.ChangeType(22, typeof(T));
return default(T);
}
GetPropertyValue<string>("age") wants to return a string. Change it to GetPropertyValue<int>("age") and it will work as long as "age" is your parameter value.
Your implementation would be better off getting the type of the generic parameter T in order to choose what to return instead of basing it on the function parameter.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace TestGener234
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("his first name is {0}", GetPropertyValue<string>("firstName"));
Console.WriteLine("his age is {0}", GetPropertyValue<int>("age"));
}
public static T GetPropertyValue<T>(string propertyIdCode)
{
if (typeof(T) == typeof(string) && propertyIdCode == "firstName")
return "Jim";
if (typeof(T) == typeof(string) && propertyIdCode == "age")
return "32";
if (typeof(T) == typeof(int) && propertyIdCode == "age")
return 32;
throw (new ArgumentException());
}
}
}
You can return object from GetPropertyValue and then do a cast. You are trying to use a generic method to return specific types depending on input parameters. Sounds confusing :-)
public static object GetPropertyValue(string propertyIdCode)
{
if (propertyIdCode == "firstName")
return "Jim";
if (propertyIdCode == "age")
return 32;
return null;
}
and then cast (int)GetPropertyValue("age");
Usually when you are casting inside a generic method, it is a design problem. Usually, you want to keep your type generic inside your method (no casting, no braching based on type), something like this:
public class Properties<T>
{
private Dictionary<string, T> _dict = new Dictionary<string, T>();
public void SetPropertyValue<T>(string propertyIdCode, T value)
{
_dict[propertyIdCode] = value;
}
public T GetPropertyValue<T>(string propertyIdCode)
{
return _dict[propertyIdCode];
}
}
On, the other hand, if you want to access object's properties through their name (it seems like this is what you are doing, sorry if I got it wrong), the right way would be to use reflection (PropertyInfo.GetValue):
public object GetPropertyValue(object obj, string propertyIdCode)
{
PropertyInfo pinfo = obj.GetType().GetProperty(propertyIdCode);
return pinfo.GetValue(obj, null);
}
public static T GetPropertyValue<T>(string propertyIdCode)
{
object result = null;
if (propertyIdCode == "firstName")
result = "Jim";
if (propertyIdCode == "age")
result = 32;
return result == null ? default(T) : (T)result;
}
Marc's example of double-casting is the correct way to get the compiler to behave correctly.
You could write a sperate method for each value type and have a generic method for reference types. This would stop stop boxing on value types.
This is only useful if the objects being accessed are not boxed for storage (e.g. not stored as an object).
public static T GetPropertyValue<T>(string propertyIdCode)
{
}
public static int GetPropertyInt(string propertyIdCode)
{
}
There is another way - by using Convert.ChangeType:
CType(Convert.ChangeType(mItem, GetType(TItem)), TItem)

Creating a generic method in C#

I am trying to combine a bunch of similar methods into a generic method. I have several methods that return the value of a querystring, or null if that querystring does not exist or is not in the correct format. This would be easy enough if all the types were natively nullable, but I have to use the nullable generic type for integers and dates.
Here's what I have now. However, it will pass back a 0 if a numeric value is invalid, and that unfortunately is a valid value in my scenarios. Can somebody help me out? Thanks!
public static T GetQueryString<T>(string key) where T : IConvertible
{
T result = default(T);
if (String.IsNullOrEmpty(HttpContext.Current.Request.QueryString[key]) == false)
{
string value = HttpContext.Current.Request.QueryString[key];
try
{
result = (T)Convert.ChangeType(value, typeof(T));
}
catch
{
//Could not convert. Pass back default value...
result = default(T);
}
}
return result;
}
What if you specified the default value to return, instead of using default(T)?
public static T GetQueryString<T>(string key, T defaultValue) {...}
It makes calling it easier too:
var intValue = GetQueryString("intParm", Int32.MinValue);
var strValue = GetQueryString("strParm", "");
var dtmValue = GetQueryString("dtmPatm", DateTime.Now); // eg use today's date if not specified
The downside being you need magic values to denote invalid/missing querystring values.
I know, I know, but...
public static bool TryGetQueryString<T>(string key, out T queryString)
What about this? Change the return type from T to Nullable<T>
public static Nullable<T> GetQueryString<T>(string key) where T : struct, IConvertible
{
T result = default(T);
if (String.IsNullOrEmpty(HttpContext.Current.Request.QueryString[key]) == false)
{
string value = HttpContext.Current.Request.QueryString[key];
try
{
result = (T)Convert.ChangeType(value, typeof(T));
}
catch
{
//Could not convert. Pass back default value...
result = default(T);
}
}
return result;
}
Convert.ChangeType() doesn't correctly handle nullable types or enumerations in .NET 2.0 BCL (I think it's fixed for BCL 4.0 though). Rather than make the outer implementation more complex, make the converter do more work for you. Here's an implementation I use:
public static class Converter
{
public static T ConvertTo<T>(object value)
{
return ConvertTo(value, default(T));
}
public static T ConvertTo<T>(object value, T defaultValue)
{
if (value == DBNull.Value)
{
return defaultValue;
}
return (T) ChangeType(value, typeof(T));
}
public static object ChangeType(object value, Type conversionType)
{
if (conversionType == null)
{
throw new ArgumentNullException("conversionType");
}
// if it's not a nullable type, just pass through the parameters to Convert.ChangeType
if (conversionType.IsGenericType && conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
{
// null input returns null output regardless of base type
if (value == null)
{
return null;
}
// it's a nullable type, and not null, which means it can be converted to its underlying type,
// so overwrite the passed-in conversion type with this underlying type
conversionType = Nullable.GetUnderlyingType(conversionType);
}
else if (conversionType.IsEnum)
{
// strings require Parse method
if (value is string)
{
return Enum.Parse(conversionType, (string) value);
}
// primitive types can be instantiated using ToObject
else if (value is int || value is uint || value is short || value is ushort ||
value is byte || value is sbyte || value is long || value is ulong)
{
return Enum.ToObject(conversionType, value);
}
else
{
throw new ArgumentException(String.Format("Value cannot be converted to {0} - current type is " +
"not supported for enum conversions.", conversionType.FullName));
}
}
return Convert.ChangeType(value, conversionType);
}
}
Then your implementation of GetQueryString<T> can be:
public static T GetQueryString<T>(string key)
{
T result = default(T);
string value = HttpContext.Current.Request.QueryString[key];
if (!String.IsNullOrEmpty(value))
{
try
{
result = Converter.ConvertTo<T>(value);
}
catch
{
//Could not convert. Pass back default value...
result = default(T);
}
}
return result;
}
You can use sort of Maybe monad (though I'd prefer Jay's answer)
public class Maybe<T>
{
private readonly T _value;
public Maybe(T value)
{
_value = value;
IsNothing = false;
}
public Maybe()
{
IsNothing = true;
}
public bool IsNothing { get; private set; }
public T Value
{
get
{
if (IsNothing)
{
throw new InvalidOperationException("Value doesn't exist");
}
return _value;
}
}
public override bool Equals(object other)
{
if (IsNothing)
{
return (other == null);
}
if (other == null)
{
return false;
}
return _value.Equals(other);
}
public override int GetHashCode()
{
if (IsNothing)
{
return 0;
}
return _value.GetHashCode();
}
public override string ToString()
{
if (IsNothing)
{
return "";
}
return _value.ToString();
}
public static implicit operator Maybe<T>(T value)
{
return new Maybe<T>(value);
}
public static explicit operator T(Maybe<T> value)
{
return value.Value;
}
}
Your method would look like:
public static Maybe<T> GetQueryString<T>(string key) where T : IConvertible
{
if (String.IsNullOrEmpty(HttpContext.Current.Request.QueryString[key]) == false)
{
string value = HttpContext.Current.Request.QueryString[key];
try
{
return (T)Convert.ChangeType(value, typeof(T));
}
catch
{
//Could not convert. Pass back default value...
return new Maybe<T>();
}
}
return new Maybe<T>();
}
I like to start with a class like this
class settings
{
public int X {get;set;}
public string Y { get; set; }
// repeat as necessary
public settings()
{
this.X = defaultForX;
this.Y = defaultForY;
// repeat ...
}
public void Parse(Uri uri)
{
// parse values from query string.
// if you need to distinguish from default vs. specified, add an appropriate property
}
This has worked well on 100's of projects. You can use one of the many other parsing solutions to parse values.

Categories