Programming is fun!
I have created my own nullable class implementation as follows:
[DataContract]
public class Nullable<T> where T : struct
{
public Nullable()
{
}
internal T value;
[DataMember]
public bool HasValue { get; set; }
[DataMember]
public T Value
{
get
{
if (!this.HasValue)
throw new Exception("Property have no value");
return this.value;
}
set
{
this.value = value;
this.HasValue = true;
}
}
public Nullable(T value)
{
Value = value;
}
public T GetValueOrDefault()
{
return this.value;
}
public T GetValueOrDefault(T defaultValue)
{
if (!this.HasValue)
return defaultValue;
return this.value;
}
public override bool Equals(object other)
{
if (!this.HasValue)
return other == null;
if (other == null)
return false;
return this.value.Equals(other);
}
public override int GetHashCode()
{
if (!this.HasValue)
return 0;
return this.Value.GetHashCode();
}
public override string ToString()
{
if (!this.HasValue)
return "";
return this.Value.ToString();
}
}
Now in my WCF service when I create function which uses List
of my custom nullable types,
[ServiceContract]
[XmlSerializerFormat]
public interface IService
{
[OperationContract]
List<Nullable<DateTime>> NullTest();
}
public class MyService : IService
{
public List<Nullable<DateTime>> NullTest()
{
return new List<Nullable<DateTime>>()
{
new Nullable<DateTime>(DateTime.Now),
new Nullable<DateTime>(DateTime.Now.AddDays(2))
};
}
}
I get following issue when above function gets called:
Managed Debugging Assistant 'FatalExecutionEngineError' has detected a problem in 'C:\Program Files (x86)\IIS Express\iisexpress.exe'.
Additional information: The runtime has encountered a fatal error. The address of the error was at 0x5bd1399e, on thread 0x2568. The error code is 0xc0000005. This error may be a bug in the CLR or in the unsafe or non-verifiable portions of user code. Common sources of this bug include user marshaling errors for COM-interop or PInvoke, which may corrupt the stack.
Help appreciated
I am using BasicHttpBinding
<service name="MyService">
<endpoint
address=""
binding="basicHttpBinding"
name="BasicHttpEndpoint"
bindingConfiguration=""
contract="IMyService">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint address="mex" binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
System.Nullabe<T>
is astruct
, not a class. Not much point in havingHasValue
when you could have a variable of your type just assigned tonull
. – juharrcshtml5
supportsNullable<T>
– Rabban