本文整理汇总了C#中IActivator.CreateInstance方法的典型用法代码示例。如果您正苦于以下问题:C# IActivator.CreateInstance方法的具体用法?C# IActivator.CreateInstance怎么用?C# IActivator.CreateInstance使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IActivator
的用法示例。
在下文中一共展示了IActivator.CreateInstance方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DecryptElement
public static XElement DecryptElement(this XElement element, IActivator activator)
{
// If no decryption necessary, return original element.
if (!DoesElementOrDescendentRequireDecryption(element))
{
return element;
}
// Deep copy the element (since we're going to mutate) and put
// it into a document to guarantee it has a parent.
var doc = new XDocument(new XElement(element));
// We remove elements from the document as we decrypt them and perform
// fix-up later. This keeps us from going into an infinite loop in
// the case of a null decryptor (which returns its original input which
// is still marked as 'requires decryption').
var placeholderReplacements = new Dictionary<XElement, XElement>();
while (true)
{
var elementWhichRequiresDecryption = doc.Descendants(XmlConstants.EncryptedSecretElementName).FirstOrDefault();
if (elementWhichRequiresDecryption == null)
{
// All encryption is finished.
break;
}
// Decrypt the clone so that the decryptor doesn't inadvertently modify
// the original document or other data structures. The element we pass to
// the decryptor should be the child of the 'encryptedSecret' element.
var clonedElementWhichRequiresDecryption = new XElement(elementWhichRequiresDecryption);
var innerDoc = new XDocument(clonedElementWhichRequiresDecryption);
string decryptorTypeName = (string)clonedElementWhichRequiresDecryption.Attribute(XmlConstants.DecryptorTypeAttributeName);
var decryptorInstance = activator.CreateInstance<IXmlDecryptor>(decryptorTypeName);
var decryptedElement = decryptorInstance.Decrypt(clonedElementWhichRequiresDecryption.Elements().Single());
// Put a placeholder into the original document so that we can continue our
// search for elements which need to be decrypted.
var newPlaceholder = new XElement("placeholder");
placeholderReplacements[newPlaceholder] = decryptedElement;
elementWhichRequiresDecryption.ReplaceWith(newPlaceholder);
}
// Finally, perform fixup.
Debug.Assert(placeholderReplacements.Count > 0);
foreach (var entry in placeholderReplacements)
{
entry.Key.ReplaceWith(entry.Value);
}
return doc.Root;
}