<p>In some languages you can override the "new" keyword to control how types are instantiated.  You can't do this directly in .NET.  However, I was wondering if there is a way to, say, handle a "Type not found" exception and manually resolve a type before whoever "new"ed up that type blows up?</p>

<p>I'm using a serializer that reads in an xml-based file and instantiates types described within it.  I don't have any control over the serializer, but I'd like to interact with the process, hopefully without writing my own appdomain host.</p>

<p>Please don't suggest alternative serialization methods.  </p>

[{'answer_id': 76902, 'author': 'MagicKat', 'author_id': 8505, 'author_profile': 'https://Stackoverflow.com/users/8505', 'pm_score': -1, 'selected': False, 'text': '<p>You should check out Reflection and the Activator class.  They will allow you to create objects from strings.  Granted, the object has to be in one of the assemblies that you have access to.</p>\n'}, {'answer_id': 76912, 'author': 'Abe Heidebrecht', 'author_id': 9268, 'author_profile': 'https://Stackoverflow.com/users/9268', 'pm_score': 4, 'selected': True, 'text': '<p>You can attach an event handler to AppDomain.CurrentDomain.AssemblyResolve to take part in the process.</p>\n\n<p>Your EventHandler should return the assembly that is responsible for the type passed in the ResolveEventArgs.</p>\n\n<p>You can read more about it at <a href="http://msdn.microsoft.com/en-us/library/system.appdomain.assemblyresolve.aspx" rel="nofollow noreferrer">MSDN</a></p>\n'}, {'answer_id': 76956, 'author': 'Peter Ritchie', 'author_id': 5620, 'author_profile': 'https://Stackoverflow.com/users/5620', 'pm_score': 1, 'selected': False, 'text': '<p>There\'s also the <a href="http://msdn.microsoft.com/en-us/library/system.appdomain.typeresolve(VS.80).aspx" rel="nofollow noreferrer">AppDomain.TypeResolve</a> event that you can override.</p>\n'}, {'answer_id': 76978, 'author': 'Anthony', 'author_id': 5599, 'author_profile': 'https://Stackoverflow.com/users/5599', 'pm_score': 1, 'selected': False, 'text': '<p><a href="http://lingpipe-blog.com/2007/06/27/select-isnt-broken-or-horses-not-zebras/" rel="nofollow noreferrer">select isn\'t broken</a> discusses how to look at it differently - the fault may be in your design not your tooling.</p>\n\n<p>I think that trying to get "new" to do something else is going to be the wrong approach.</p>\n\n<p>Think of why operator overloading has to be used with caution - it\'s counter-intuitive and hard to debug when there are hidden changes in the language semantics.</p>\n\n<p>Step back and look at the design in a larger context, try to find a more sensible way to solve the problem.</p>\n'}]
['https://Stackoverflow.com/questions/76864', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']