Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/10/10 13:39:32 (15 years ago)
Author:
epitzer
Message:

unwrap exceptions during constructor call when deserializing storable objects (#548)

Location:
trunk/sources/HeuristicLab.Persistence/3.3
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Persistence/3.3/Default/CompositeSerializers/Storable/StorableSerializer.cs

    r2983 r2990  
    4040
    4141    public object CreateInstance(Type type, IEnumerable<Tag> metaInfo) {
    42       object instance = StorableConstructorAttribute.CallStorableConstructor(type);
    43       if (instance == null)
    44         instance = Activator.CreateInstance(type, true);
    45       return instance;
     42      try {
     43        object instance = StorableConstructorAttribute.CallStorableConstructor(type);
     44        if (instance == null)
     45          instance = Activator.CreateInstance(type, true);
     46        return instance;
     47      } catch (TargetInvocationException x) {
     48        throw new PersistenceException(
     49          "Could not instantiate storable object: Encountered exception during constructor call",
     50          x.InnerException);
     51      }
    4652    }
    4753
  • trunk/sources/HeuristicLab.Persistence/3.3/Tests/UseCases.cs

    r2983 r2990  
    715715    }
    716716
     717    [EmptyStorableClass]
     718    public class ExplodingDefaultConstructor {
     719      public ExplodingDefaultConstructor() {
     720        throw new Exception("this constructor will always fail");
     721      }
     722      public ExplodingDefaultConstructor(string password) {
     723      }
     724    }
     725
     726    [TestMethod]
     727    public void TestConstructorExceptionUnwrapping() {
     728      ExplodingDefaultConstructor x = new ExplodingDefaultConstructor("password");
     729      XmlGenerator.Serialize(x, tempFile);
     730      try {
     731        ExplodingDefaultConstructor newX = (ExplodingDefaultConstructor)XmlParser.Deserialize(tempFile);
     732        Assert.Fail("Exception expected");
     733      } catch (PersistenceException pe) {
     734        Assert.AreEqual(pe.InnerException.Message, "this constructor will always fail");
     735      }
     736    }
     737
    717738    [ClassInitialize]
    718739    public static void Initialize(TestContext testContext) {
Note: See TracChangeset for help on using the changeset viewer.