【问题标题】:Creating an instance of SqlCeLockTimeoutException创建 SqlCeLockTimeoutException 的实例
【发布时间】:2015-10-08 12:47:26
【问题描述】:

出于单元测试的目的,我想创建一个SqlCeLockTimeoutException 类型的模拟对象

由于它的构造函数受到保护,我在测试中创建了一个扩展类:

[Serializable]
private class TestableSqlCeLockTimeoutException : SqlCeLockTimeoutException
{
    public TestableSqlCeLockTimeoutException()
           : this(new SerializationInfo(typeof (TestableSqlCeLockTimeoutException), 
                                        new FormatterConverter()), 
                  new StreamingContext())
    {
    }

    protected TestableSqlCeLockTimeoutException(SerializationInfo info,
                                                StreamingContext context) 
           : base(info, context)
    {
    }
}

但是,我在创建实例时不断收到以下异常:

测试方法Foo抛出异常:
System.Runtime.Serialization.SerializationException:找不到成员“ClassName”。

为什么我一直收到它?我尝试使用Serializable 属性无济于事。

任何解决方法都会有所帮助。

【问题讨论】:

    标签: c# unit-testing serialization serializable sql-server-ce-4


    【解决方案1】:

    @Mugan 你问了一个非常有趣的问题!为了回答这个问题,我不得不反编译SqlCeLockTimeoutException。当我试图帮助你时,我学到了很多东西。谢谢。

    出现问题是因为System.ExceptionC`tor 需要反序列化一些属性:

    [System.Security.SecuritySafeCritical]  // auto-generated
    protected Exception(SerializationInfo info, StreamingContext context) 
    {
     //some validation
        _className = info.GetString("ClassName");
        _message = info.GetString("Message");
        _data = (IDictionary)(info.GetValueNoThrow("Data",typeof(IDictionary)));
        _innerException = (Exception)(info.GetValue("InnerException",typeof(Exception)));
        _helpURL = info.GetString("HelpURL");
        _stackTraceString = info.GetString("StackTraceString");
        _remoteStackTraceString = info.GetString("RemoteStackTraceString");
        _remoteStackIndex = info.GetInt32("RemoteStackIndex");
        _exceptionMethodString = (String)(info.GetValue("ExceptionMethod",typeof(String)));
        HResult = info.GetInt32("HResult");
        _source = info.GetString("Source");
    //some bla bla....
    

    为了解决上述问题,我将受保护的 C`tor 更改为 public,然后使用它:

    var info = new SerializationInfo(typeof (TestableSqlCeLockTimeoutException), 
               new FormatterConverter());
    info.AddValue("ClassName", string.Empty);
    info.AddValue("Message", string.Empty);
    info.AddValue("InnerException", new ArgumentException());
    info.AddValue("HelpURL", string.Empty);
    info.AddValue("StackTraceString", string.Empty);
    info.AddValue("RemoteStackTraceString", string.Empty);
    info.AddValue("RemoteStackIndex", 0);
    info.AddValue("ExceptionMethod", string.Empty);
    info.AddValue("HResult", 1);
    info.AddValue("Source", string.Empty);
    new TestableSqlCeLockTimeoutException(info,new StreamingContext());
    

    然后这次从SqlCeException 引发了新异常。 SqlCeExceptionCtor 还需要一个属性__Errors__:

    protected SqlCeException(SerializationInfo info, StreamingContext context)
      : base(info, context)
    {
      if (info == null)
        throw new ArgumentNullException("info");
      this.Errors = (SqlCeErrorCollection) info.GetValue("__Errors__", typeof (SqlCeErrorCollection));
    }
    

    SqlCeErrorCollectionCtor 是内部的,所以我用Reflaction 创建了一个实例:

    BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance;
    CultureInfo culture = null; // use InvariantCulture or other if you prefer
    object instantiatedType =
           Activator.CreateInstance(typeof(SqlCeErrorCollection), flags, null, null, culture);
    info.AddValue("__Errors__", instantiatedType);
    

    现在一切正常。我最终得到了一个创建方法:

    private static SqlCeLockTimeoutException CreateSqlCeLockTimeoutExceptionForTest()
    {
        var info = new SerializationInfo(typeof (TestableSqlCeLockTimeoutException), 
                   new FormatterConverter());
        info.AddValue("ClassName", string.Empty);
        info.AddValue("Message", string.Empty);
        info.AddValue("InnerException", new ArgumentException());
        info.AddValue("HelpURL", string.Empty);
        info.AddValue("StackTraceString", string.Empty);
        info.AddValue("RemoteStackTraceString", string.Empty);
        info.AddValue("RemoteStackIndex", 0);
        info.AddValue("ExceptionMethod", string.Empty);
        info.AddValue("HResult", 1);
        info.AddValue("Source", string.Empty);
        BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance;
        CultureInfo culture = null; // use InvariantCulture or other if you prefer
        object instantiatedType = Activator.CreateInstance(typeof (SqlCeErrorCollection), 
                                    flags, null, null, culture);
        info.AddValue("__Errors__", instantiatedType);
        return new TestableSqlCeLockTimeoutException(info, new StreamingContext());
    }
    

    您还可以使用Reflaction 来创建SqlCeLockTimeoutException 的新实例,而不是TestableSqlCeLockTimeoutException:

    private static SqlCeLockTimeoutException CreateSqlCeLockTimeoutExceptionForTest()
    {
        BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance;
        CultureInfo culture = null; // use InvariantCulture or other if you prefer
        object instantiatedType = Activator.CreateInstance(typeof (SqlCeErrorCollection), 
                      flags, null, null, culture);
    
        object result = Activator.CreateInstance(typeof(SqlCeLockTimeoutException), 
                      flags, null, new []{instantiatedType}, culture);
    
        return (SqlCeLockTimeoutException)result;
    }
    

    【讨论】:

      【解决方案2】:
          [Fact]
          public void ConstructTest2()
          {
              var info = new SerializationInfo(typeof(CvmpApplicationException), new MyFormatConverter());
      
              info.AddValue("ClassName", typeof(CvmpApplicationException).FullName);
              info.AddValue("Message", "test");
              info.AddValue("InnerException", new Exception());
              info.AddValue("HelpURL", string.Empty);
              info.AddValue("StackTraceString", string.Empty);
              info.AddValue("RemoteStackTraceString", string.Empty);
              info.AddValue("RemoteStackIndex", 0);
              info.AddValue("ExceptionMethod", string.Empty);
              info.AddValue("HResult", 1);
              info.AddValue("Source", string.Empty);
      
              _ = new TestCvmpApplicationException(
                  info,
                  new StreamingContext(StreamingContextStates.All));
          }
      
          private class MyFormatConverter : IFormatterConverter
          {
              public object Convert(object value, Type type)
              {
                  return value;
              }
      
              public object Convert(object value, TypeCode typeCode)
              {
                  return value;
              }
      
              public bool ToBoolean(object value)
              {
                  return true;
              }
      
              public byte ToByte(object value)
              {
                  return 1;
              }
      
              public char ToChar(object value)
              {
                  throw new NotImplementedException();
              }
      
              public DateTime ToDateTime(object value)
              {
                  return DateTime.Now;
              }
      
              public decimal ToDecimal(object value)
              {
                  return 1;
              }
      
              public double ToDouble(object value)
              {
                  return 1D;
              }
      
              public short ToInt16(object value)
              {
                  return 1;
              }
      
              public int ToInt32(object value)
              {
                  return 1;
              }
      
              public long ToInt64(object value)
              {
                  return 1;
              }
      
              public sbyte ToSByte(object value)
              {
                  return 1;
              }
      
              public float ToSingle(object value)
              {
                  return 1;
              }
      
              public string? ToString(object value)
              {
                  return "Message";
              }
      
              public ushort ToUInt16(object value)
              {
                  return 1;
              }
      
              public uint ToUInt32(object value)
              {
                  return 1;
              }
      
              public ulong ToUInt64(object value)
              {
                  return 1;
              }
          }
      
          private class TestCvmpApplicationException : CvmpApplicationException
          {
              public TestCvmpApplicationException(SerializationInfo info, StreamingContext context)
                  : base(info, context)
              {
              }
          }
      

      【讨论】:

      • 请记住,Stack Overflow 不仅仅是为了解决眼前的问题,而是为了帮助未来的读者找到类似问题的解决方案,这需要了解底层代码。这对于我们社区的初学者和不熟悉语法的成员来说尤其重要。鉴于此,您能否edit 您的答案包括对您正在做什么的解释以及为什么您认为这是最好的方法?
      猜你喜欢
      • 2012-08-28
      • 2012-08-28
      • 2016-09-04
      • 1970-01-01
      • 2021-04-16
      • 2018-01-22
      • 1970-01-01
      相关资源
      最近更新 更多