views:

307

answers:

7

Hi there,

I want to create an error class. And it has some static properties. For example : Message, InnerException, Stacktrace, Source. But i want to add some dinamiclly properties. If exception is a FileNotFoundException, i want add FileName property. Or if it s a SqlException, want to add LineNumber property. And i cant inherit that class from Exception because, i return that class from a web service. How can i do that?

Thanx for your helps.

A: 

It's very unclear what you're trying to achieve. Reconsider your design in favor of non-static classes and use inheritance.

And you do remember that there are lots of ready-made exception classes in the .NET BCL, right?

Anton Gogolev
+1  A: 

C# is a statically typed language. This means you generally cannot dynamically add properties to classes at run-time without some really funky IL injection (which you definitely want to avoid).

In your case it seems that you need to understand exceptions a bit better - we usually throw a specific type of exception to indicate the cause of an exceptional problem. For example, if you are looking for a file and it's not there you would throw a FileNotFoundException or if there is some application-specific problem you could create your own exception class and throw that exception.

Keep in mind that exceptions should be exceptional.

Jaco Pretorius
ever heard of anonymous types?
Lucas B
Yeah I've heard of it - you can't create anonymous exceptions though
Jaco Pretorius
A: 

Check out .NET Reflection Create Class Properties

SwDevMan81
+2  A: 

Instead of trying to do something C# currently doesn't handle very well, I suggest you take a somewhat different approach.

Why don't you add those extra properties as data in a dictionary? Your class could expose an interface to get a number of "properties" (in the general sense of the word), i.e. the keys and your calling code could then examine these and use them to look up values as necessary.

Brian Rasmussen
A: 

Create a new exception type, derived from Exception. Throw this exception, and set the InnerException property to the exception that caused all the ruckus.

You can check for the type of the inner exception and display data accordingly.

If possible, you could of course also just throw the original excpetion to have the caller handling it.

mafutrct
+1  A: 

you can create type dynamically using new features in C# like anonymous types

I am not sure if you are trying to do some thing similar, but can achieve the requirement as follows

        public interface IError { }

        public class ErrorTypeA : IError
        { public string Name; }

        public class ErrorTypeB : IError
        {
            public string Name;
            public int line;
        }

        public void CreateErrorObject()
        {
            IError error;
            if (FileNotFoundException) // put your check here
            {
                error = new ErrorTypeA
                    {
                        Name = ""
                    };
            }
            elseif (InValidOpertionException) // put your check here
            {
                error = new ErrorTypeB
                {
                    Name = "",
                    line = 1
                };
            }
        }

Hope this helps

Asad Butt
So you're catching the exception and wrapping it?? That's a very bad way of dealing with exceptions (the calling code will always have to check the return value and deal with the exception). This makes the consumer code trickier than it should be.
Jaco Pretorius
The point for this Poster is not best practice for Exception handling!please review the question again
Asad Butt
+1  A: 

My take on this would be to use a dictionary where you can store all extra data.

public class Logging
{
    private Dictionary<string, string> _ExtraInfo = new Dictionary<string, string>();

    public Dictionary<string, string> ExtraInfo {
        get { return _ExtraInfo; }
        set { _ExtraInfo = value; }
    }

}

To use:

   Logging MyLogger = new Logging();
   MyLogger.ExtraInfo.Add("Filename", thefilename);
   MyLogger.ExtraInfo.Add("ClientTime", now);

And so on.

Stefan