Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Possible to have a generic return type?

Tags:

c#

Here is a typical function that returns either true/false;

private static bool hasValue()
{
    return true; 
}

Now on an error, I would like to return my own custom error object with definition:

public class Failure
{
    public string FailureDateTime { get; set; }
    public string FailureReason { get; set; }
}

I would have expected to be able to throw this custom object for example...

private static bool hasValue() 
{
    try 
    {
        // do something
    }
    catch 
    {
        throw new Failure();
    }
    
    return true;
}

This is not possible, and I don't want to derive Failure from System.IO.Exception because I have personally had issues serializing exceptions in C# (This was with .net v2).

What is the best practice / or ideal solution to this problem. Should I just work with private static object? Or is there a cleaner way to return a custom object or bypass the typical return type on an error (not using System.IO.Exception)?

I am not entirely wild about using an object either, because then I need to validate the result by using casting and more boolean.

like image 473
JL. Avatar asked Jun 03 '10 11:06

JL.


People also ask

What is the full name of C?

In the real sense it has no meaning or full form. It was developed by Dennis Ritchie and Ken Thompson at AT&T bell Lab. First, they used to call it as B language then later they made some improvement into it and renamed it as C and its superscript as C++ which was invented by Dr.

What do you mean by C?

C is a structured, procedural programming language that has been widely used both for operating systems and applications and that has had a wide following in the academic community. Many versions of UNIX-based operating systems are written in C.

Is C language easy?

C is a general-purpose language that most programmers learn before moving on to more complex languages. From Unix and Windows to Tic Tac Toe and Photoshop, several of the most commonly used applications today have been built on C. It is easy to learn because: A simple syntax with only 32 keywords.

What is C language used for?

C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...


3 Answers

This kind of solution may fit to this case:

var exception = new Exception();
exception.Data.Add("Failure", new Failure());
throw exception;

You can throw the exception (recommended to define your own exception type), and use the Data dictionary to hold the Failure. In the catch code you can take the data and serialize it. If you do create your own exception type you can place the Failure on a property.

Comment: I didn't check it, but are you sure exceptions are not serializable?

like image 175
Elisha Avatar answered Oct 22 '22 03:10

Elisha


I don't want to derive Failure from System.IO.Exception because of the inability to serialize an exception in C#.

The assumption that this question is based on is that you cannot serialize exceptions derived from IOException. However I wish to challenge this assumption. IOException is marked as Serializable. All you have to do is mark your subclass as serializable too.

See the documentation for IOException:

[SerializableAttribute]
[ComVisibleAttribute(true)]
public class IOException : SystemException
like image 34
Mark Byers Avatar answered Oct 22 '22 05:10

Mark Byers


You need to derive from System.Exception (or any other non-sealed subclass of it) if you want to create your own exception class. Failure to serialize that you are talking about is likely the fact that you didn't implement the serialization constructor.

Visual Studio has a code snippet that creates a recommended template for subclassing exception. You can get it by typing exception and pressing tab twice.

This is what comes out:

[Serializable]
public class MyException : Exception {
    public MyException() { }
    public MyException(string message) : base(message) { }
    public MyException(string message, Exception inner) : base(message, inner) { }
    protected MyException(
      System.Runtime.Serialization.SerializationInfo info,
      System.Runtime.Serialization.StreamingContext context)
        : base(info, context) { }
}
like image 25
Igor Zevaka Avatar answered Oct 22 '22 04:10

Igor Zevaka