本文整理汇总了C#中System.Exception.GetObjectData方法的典型用法代码示例。如果您正苦于以下问题:C# Exception.GetObjectData方法的具体用法?C# Exception.GetObjectData怎么用?C# Exception.GetObjectData使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Exception
的用法示例。
在下文中一共展示了Exception.GetObjectData方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: PreserveStackTrace
private void PreserveStackTrace(Exception exception)
{
var context = new StreamingContext(StreamingContextStates.CrossAppDomain);
var serializationInfo = new SerializationInfo(typeof(Exception), new FormatterConverter());
var constructor = typeof(Exception).GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, new[] { typeof(SerializationInfo), typeof(StreamingContext) }, null);
exception.GetObjectData(serializationInfo, context);
constructor.Invoke(exception, new object[] { serializationInfo, context });
}
示例2: PreserveStackTrace
private void PreserveStackTrace(Exception exception)
{
var context = new StreamingContext(StreamingContextStates.CrossAppDomain);
var objectManager = new ObjectManager(null, context);
var serializationInfo = new SerializationInfo(exception.GetType(), new FormatterConverter());
exception.GetObjectData(serializationInfo, context);
objectManager.RegisterObject(exception, 1, serializationInfo);
objectManager.DoFixups();
}
示例3: PreserveStackTrace
/// <remarks>
/// Credit to MvcContrib.TestHelper.AssertionException for PreserveStackTrace
/// </remarks>
private static void PreserveStackTrace(Exception e)
{
var ctx = new StreamingContext(StreamingContextStates.CrossAppDomain);
var mgr = new ObjectManager(null, ctx);
var si = new SerializationInfo(e.GetType(), new FormatterConverter());
e.GetObjectData(si, ctx);
mgr.RegisterObject(e, 1, si);
mgr.DoFixups();
}
示例4: PreserveStackTrace
public static void PreserveStackTrace(Exception e)
{
var ctx = new StreamingContext(StreamingContextStates.CrossAppDomain);
var mgr = new ObjectManager(null, ctx);
var si = new SerializationInfo(e.GetType(), new FormatterConverter());
e.GetObjectData(si, ctx);
mgr.RegisterObject(e, 1, si); // prepare for SetObjectData
mgr.DoFixups(); // ObjectManager calls SetObjectData
// voila, e is unmodified save for _remoteStackTraceString
}
示例5: ToExceptionJsonObject
private JsonObject ToExceptionJsonObject(Exception e, int depth)
{
var o = new JsonObject();
#if !DOTNETCORE
var si = new SerializationInfo(e.GetType(), new FormatterConverter());
var sc = new StreamingContext();
e.GetObjectData(si, sc);
//TODO Loop over ISerializable data
var helpUrl = si.GetString("HelpURL");
var stackTrace = si.GetString("StackTraceString");
var remoteStackTrace = si.GetString("RemoteStackTraceString");
var remoteStackIndex = si.GetInt32("RemoteStackIndex");
var exceptionMethod = si.GetString("ExceptionMethod");
var hresult = si.GetInt32("HResult");
var source = si.GetString("Source");
var className = si.GetString("ClassName");
#else
var helpUrl = e.HelpLink;
var stackTrace = e.StackTrace;
var remoteStackTrace = string.Empty;
var remoteStackIndex = string.Empty;
var exceptionMethod = string.Empty;
var hresult = e.HResult;
var source = e.Source;
var className = string.Empty;
#endif
o.Add("Depth", depth);
o.Add("ClassName", className);
o.Add("Message", e.Message);
o.Add("Source", source);
o.Add("StackTraceString", stackTrace);
o.Add("RemoteStackTraceString", remoteStackTrace);
o.Add("RemoteStackIndex", remoteStackIndex);
o.Add("HResult", hresult);
o.Add("HelpURL", helpUrl);
#if !DOTNETCORE
this.WriteStructuredExceptionMethod(o, exceptionMethod);
#endif
return o;
}
示例6: InternalPreserveStackTrace
private static void InternalPreserveStackTrace(Exception e)
{
// check if method is applicable (exception type should have the deserialization constructor)
var bindingFlags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance;
var constructor = e.GetType().GetConstructor(bindingFlags, null, new[] { typeof(SerializationInfo), typeof(StreamingContext) }, null);
if (constructor == null)
{
return;
}
var ctx = new StreamingContext(StreamingContextStates.CrossAppDomain);
var mgr = new ObjectManager(null, ctx);
var si = new SerializationInfo(e.GetType(), new FormatterConverter());
e.GetObjectData(si, ctx);
mgr.RegisterObject(e, 1, si); // prepare for SetObjectData
mgr.DoFixups(); // ObjectManager calls the deserialization constructor
// voila, e is unmodified save for _remoteStackTraceString
}
示例7: GetObjectData_Null
public void GetObjectData_Null ()
{
Exception e = new Exception ();
e.GetObjectData (null, new StreamingContext (StreamingContextStates.All));
}
示例8: Create
/// <summary>
/// Constructs the exception details for the server to return
/// </summary>
public static RpcExceptionInfo Create(Exception error, RpcErrorDetailBehavior details)
{
if (details == RpcErrorDetailBehavior.NoDetails)
{
return DefaultInstance;
}
Builder builder = CreateBuilder();
SerializationInfo si = new SerializationInfo(error.GetType(), FormatterConverter);
if (details == RpcErrorDetailBehavior.MessageOnly)
{
si.AddValue("Message", error.Message);
}
else if (details == RpcErrorDetailBehavior.FullDetails)
{
try
{
error.GetObjectData(si, StreamingContext);
builder.SetHasFullDetails(true);
}
catch
{
}
}
builder.AssemblyName = si.AssemblyName;
builder.FullTypeName = si.FullTypeName;
foreach (SerializationEntry se in si)
{
switch (se.Name)
{
case "ClassName":
if (se.Value is string)
{
builder.SetClassName((string) se.Value);
}
break;
case "Message":
if (se.Value is string)
{
builder.SetMessage((string) se.Value);
}
break;
//case "Data": if (se.Value is string) builder.SetData((string)se.Value); break;
case "InnerException":
if (se.Value is Exception)
{
builder.SetInnerException(Create((Exception) se.Value, details));
}
break;
case "HelpURL":
if (se.Value is string)
{
builder.SetHelpUrl((string) se.Value);
}
break;
case "StackTraceString":
if (se.Value is string)
{
builder.SetStackTraceString((string) se.Value);
}
break;
case "RemoteStackTraceString":
if (se.Value is string)
{
builder.SetRemoteStackTraceString((string) se.Value);
}
break;
case "RemoteStackIndex":
if (se.Value is int)
{
builder.SetRemoteStackIndex((int) se.Value);
}
break;
case "ExceptionMethod":
if (se.Value is string)
{
builder.SetExceptionMethod((string) se.Value);
}
break;
case "HResult":
if (se.Value is int)
{
builder.SetHResult((int) se.Value);
}
break;
case "Source":
if (se.Value is string)
{
builder.SetSource((string) se.Value);
}
break;
default:
{
if (se.ObjectType == typeof (String) || //se.ObjectType == typeof(byte[]) ||
//.........这里部分代码省略.........
示例9: WriteExceptionSerializationInfo
private void WriteExceptionSerializationInfo(Exception exception, ref string delim, TextWriter output, int depth)
{
var si = new SerializationInfo(exception.GetType(), new FormatterConverter());
var sc = new StreamingContext();
exception.GetObjectData(si, sc);
var helpUrl = si.GetString("HelpURL");
var stackTrace = si.GetString("StackTraceString");
var remoteStackTrace = si.GetString("RemoteStackTraceString");
var remoteStackIndex = si.GetInt32("RemoteStackIndex");
var exceptionMethod = si.GetString("ExceptionMethod");
var hresult = si.GetInt32("HResult");
var source = si.GetString("Source");
var className = si.GetString("ClassName");
var watsonBuckets = si.GetValue("WatsonBuckets", typeof(byte[])) as byte[];
//TODO Loop over ISerializable data
output.Write(delim);
output.Write("{");
delim = "";
this.WriteJsonProperty("Depth", depth, ref delim, output);
this.WriteJsonProperty("ClassName", className, ref delim, output);
this.WriteJsonProperty("Message", exception.Message, ref delim, output);
this.WriteJsonProperty("Source", source, ref delim, output);
this.WriteJsonProperty("StackTraceString", stackTrace, ref delim, output);
this.WriteJsonProperty("RemoteStackTraceString", remoteStackTrace, ref delim, output);
this.WriteJsonProperty("RemoteStackIndex", remoteStackIndex, ref delim, output);
this.WriteStructuredExceptionMethod(exceptionMethod, ref delim, output);
this.WriteJsonProperty("HResult", hresult, ref delim, output);
this.WriteJsonProperty("HelpURL", helpUrl, ref delim, output);
//writing byte[] will fall back to serializer and they differ in output
//JsonNET assumes string, simplejson writes array of numerics.
//Skip for now
//this.WriteJsonProperty("WatsonBuckets", watsonBuckets, ref delim, output);
output.Write("}");
delim = ",";
if (exception.InnerException != null && depth < 20)
this.WriteExceptionSerializationInfo(exception.InnerException, ref delim, output, ++depth);
}
示例10: GetObjectData_Info_Null
public void GetObjectData_Info_Null ()
{
Exception e = new Exception ();
try {
e.GetObjectData (null, new StreamingContext ());
Assert.Fail ("#1");
} catch (ArgumentNullException ex) {
Assert.AreEqual (typeof (ArgumentNullException), ex.GetType (), "#2");
Assert.IsNull (ex.InnerException, "#3");
Assert.IsNotNull (ex.Message, "#4");
Assert.AreEqual ("info", ex.ParamName, "#5");
}
}
示例11: GetObjectData
public void GetObjectData ()
{
string msg = "MESSAGE";
Exception inner = new ArgumentException ("whatever");
SerializationInfo si;
Exception se;
se = new Exception (msg, inner);
si = new SerializationInfo (typeof (Exception),
new FormatterConverter ());
se.GetObjectData (si, new StreamingContext ());
Assert.AreEqual (11, si.MemberCount, "#A1");
Assert.AreEqual (typeof (Exception).FullName, si.GetString ("ClassName"), "#A2");
Assert.IsNull (si.GetValue ("Data", typeof (IDictionary)), "#A3");
Assert.AreSame (msg, si.GetString ("Message"), "#A4");
Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#A5");
Assert.AreSame (se.HelpLink, si.GetString ("HelpURL"), "#A6");
Assert.IsNull (si.GetString ("StackTraceString"), "#A7");
Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#A8");
Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#A9");
Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#A10");
Assert.IsNull (si.GetString ("Source"), "#A11");
Assert.IsNull (si.GetString ("ExceptionMethod"), "#A12");
// attempt initialization of lazy init members
Assert.IsNotNull (se.Data);
Assert.IsNull (se.Source);
Assert.IsNull (se.StackTrace);
si = new SerializationInfo (typeof (Exception),
new FormatterConverter ());
se.GetObjectData (si, new StreamingContext ());
Assert.AreEqual (11, si.MemberCount, "#B1");
Assert.AreEqual (typeof (Exception).FullName, si.GetString ("ClassName"), "#B2");
Assert.AreSame (se.Data, si.GetValue ("Data", typeof (IDictionary)), "#B3");
Assert.AreSame (msg, si.GetString ("Message"), "#B4");
Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#B5");
Assert.AreSame (se.HelpLink, si.GetString ("HelpURL"), "#B6");
Assert.IsNull (si.GetString ("StackTraceString"), "#B7");
Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#B8");
Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#B9");
Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#B10");
Assert.IsNull (si.GetString ("Source"), "#B11");
Assert.IsNull (si.GetString ("ExceptionMethod"), "#B12");
try {
throw new Exception (msg, inner);
} catch (Exception ex) {
si = new SerializationInfo (typeof (Exception),
new FormatterConverter ());
ex.GetObjectData (si, new StreamingContext ());
Assert.AreEqual (11, si.MemberCount, "#C1");
Assert.AreEqual (typeof (Exception).FullName, si.GetString ("ClassName"), "#C2");
Assert.IsNull (si.GetValue ("Data", typeof (IDictionary)), "#C3");
Assert.AreSame (msg, si.GetString ("Message"), "#C4");
Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#C5");
Assert.AreSame (se.HelpLink, si.GetString ("HelpURL"), "#C6");
Assert.IsNotNull (si.GetString ("StackTraceString"), "#C7");
Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#C8");
Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#C9");
Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#C10");
Assert.IsNotNull (si.GetString ("Source"), "#C11");
//Assert.IsNotNull (si.GetString ("ExceptionMethod"), "#C12");
}
try {
throw new Exception (msg, inner);
} catch (Exception ex) {
// force initialization of lazy init members
Assert.IsNotNull (ex.Data);
Assert.IsNotNull (ex.StackTrace);
si = new SerializationInfo (typeof (Exception),
new FormatterConverter ());
ex.GetObjectData (si, new StreamingContext ());
Assert.AreEqual (11, si.MemberCount, "#D1");
Assert.AreEqual (typeof (Exception).FullName, si.GetString ("ClassName"), "#D2");
Assert.AreSame (ex.Data, si.GetValue ("Data", typeof (IDictionary)), "#D3");
Assert.AreSame (msg, si.GetString ("Message"), "#D4");
Assert.AreSame (inner, si.GetValue ("InnerException", typeof (Exception)), "#D5");
Assert.AreSame (ex.HelpLink, si.GetString ("HelpURL"), "#D6");
Assert.IsNotNull (si.GetString ("StackTraceString"), "#D7");
Assert.IsNull (si.GetString ("RemoteStackTraceString"), "#D8");
Assert.AreEqual (0, si.GetInt32 ("RemoteStackIndex"), "#D9");
Assert.AreEqual (-2146233088, si.GetInt32 ("HResult"), "#D10");
Assert.AreEqual (typeof (ExceptionTest).Assembly.GetName ().Name, si.GetString ("Source"), "#D11");
//Assert.IsNotNull (si.GetString ("ExceptionMethod"), "#D12");
}
}
示例12: PrepareExceptionForRethrow
/// <summary>
/// Causes the original strack trace of the exception to be preserved when it is rethrown
/// </summary>
/// <param name="ex"></param>
private static void PrepareExceptionForRethrow(Exception ex)
{
var ctx = new StreamingContext(StreamingContextStates.CrossAppDomain);
var mgr = new ObjectManager(null, ctx);
var si = new SerializationInfo(ex.GetType(), new FormatterConverter());
ex.GetObjectData(si, ctx);
mgr.RegisterObject(ex, 1, si); // prepare for SetObjectData
mgr.DoFixups(); // ObjectManager calls SetObjectData
}
示例13: WriteSingleException
/// <summary>
/// Writes the properties of a single exception, without inner exceptions
/// Callers are expected to open and close the json object themselves.
/// </summary>
/// <param name="exception"></param>
/// <param name="delim"></param>
/// <param name="output"></param>
/// <param name="depth"></param>
protected void WriteSingleException(Exception exception, ref string delim, TextWriter output, int depth)
{
#if NO_SERIALIZATION
var helpUrl = exception.HelpLink;
var stackTrace = exception.StackTrace;
var remoteStackTrace = string.Empty;
var remoteStackIndex = -1;
var exceptionMethod = string.Empty;
var hresult = exception.HResult;
var source = exception.Source;
var className = string.Empty;
#else
var si = new SerializationInfo(exception.GetType(), new FormatterConverter());
var sc = new StreamingContext();
exception.GetObjectData(si, sc);
var helpUrl = si.GetString("HelpURL");
var stackTrace = si.GetString("StackTraceString");
var remoteStackTrace = si.GetString("RemoteStackTraceString");
var remoteStackIndex = si.GetInt32("RemoteStackIndex");
var exceptionMethod = si.GetString("ExceptionMethod");
var hresult = si.GetInt32("HResult");
var source = si.GetString("Source");
var className = si.GetString("ClassName");
#endif
//TODO Loop over ISerializable data
this.WriteJsonProperty("Depth", depth, ref delim, output);
this.WriteJsonProperty("ClassName", className, ref delim, output);
this.WriteJsonProperty("Message", exception.Message, ref delim, output);
this.WriteJsonProperty("Source", source, ref delim, output);
this.WriteJsonProperty("StackTraceString", stackTrace, ref delim, output);
this.WriteJsonProperty("RemoteStackTraceString", remoteStackTrace, ref delim, output);
this.WriteJsonProperty("RemoteStackIndex", remoteStackIndex, ref delim, output);
this.WriteStructuredExceptionMethod(exceptionMethod, ref delim, output);
this.WriteJsonProperty("HResult", hresult, ref delim, output);
this.WriteJsonProperty("HelpURL", helpUrl, ref delim, output);
//writing byte[] will fall back to serializer and they differ in output
//JsonNET assumes string, simplejson writes array of numerics.
//Skip for now
//this.WriteJsonProperty("WatsonBuckets", watsonBuckets, ref delim, output);
}