How can I add a property to a class dynamically

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 dynamic properties.

If the exception is FileNotFoundException , I want to add the FileName property.

Or, if it is a SqlException , I want to add the LineNumber property. And I cannot inherit this class from Exception because I am returning this class from a web service. How can i do this?

+3
c # class exception-handling
source share
7 answers

you can dynamically create a type using new features in C #, such as anonymous types

I'm not sure if you are trying to do something 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

+1
source share

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

In your case, it seems that you need to understand exceptions a little better - we usually throw a specific type of exception to indicate the cause of the exceptional problem. For example, if you are looking for a file and it does not exist, you would choose a FileNotFoundException exception, or if there is any specific problem, you can create your own exception class and throw this exception.

Keep in mind that exceptions must be exceptional.

+2
source share

Instead of trying to do something, C # is currently not doing very well, I offer you a slightly different approach.

Why don't you add these additional properties as data in the dictionary? Your class could open an interface to get a number of “properties” (in the general sense of the word), that is, the keys and call code could then examine them and use them to find values ​​as needed.

+2
source share

My activity will be to use a dictionary in which you can store all the additional data.

 public class Logging { private Dictionary<string, string> _ExtraInfo = new Dictionary<string, string>(); public Dictionary<string, string> ExtraInfo { get { return _ExtraInfo; } set { _ExtraInfo = value; } } } 

For use:

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

And so on.

+1
source share

It is very unclear what you are trying to achieve. Revise your design for non-static classes and use inheritance.

And do you remember that there are many ready-made exception classes in .NET BCL?

0
source share

Check Out .NET Reflection Create Class Properties

0
source share

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

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

If possible, you can, of course, also just throw original excpetion so that the handler calls it.

0
source share

All Articles