public static object CloneDirectoryData(object data) { if (data == null) { return(null); } if (data.GetType() == typeof(RawSecurityDescriptor)) { int binaryLength = ((RawSecurityDescriptor)data).BinaryLength; byte[] binaryForm = new byte[binaryLength]; ((RawSecurityDescriptor)data).GetBinaryForm(binaryForm, 0); return(new RawSecurityDescriptor(binaryForm, 0)); } if (data is ICloneable) { return(((ICloneable)data).Clone()); } if (data.GetType() == typeof(SecurityIdentifier) || data.GetType() == typeof(PSCredential)) { return(data); } ImmutableObjectAttribute immutableObjectAttribute = (ImmutableObjectAttribute)data.GetType().GetTypeInfo().GetCustomAttribute(typeof(ImmutableObjectAttribute)); if (immutableObjectAttribute != null && immutableObjectAttribute.Immutable) { return(data); } object result; try { result = CloneHelper.SerializeObj(data); } catch (SerializationException) { throw new CannotCloneException(DataStrings.DataNotCloneable(data.GetType().Name)); } return(result); }