我了解如何将类型类分配给变量:
Type type = ("System.Security.AccessControl.FileSecurity").GetType();
然后我如何使用这个变量作为已经实例化对象的引用?
如果我尝试以下操作:
type instance = new SomeOtherType();
我收到以下错误代码:
找不到类型或命名空间名称“type”(您是否缺少 using 指令或程序集引用?)
例子:
FileSecurity fSecurity = fInfo.GetAccessControl();
我希望能够做到:
Type sometype = ("System.Security.AccessControl.FileSecurity").GetType();
sometype mynewtype = fInfo.GetAccessControl();
编辑: 为了更好地解释为什么我什至首先尝试这样做,请查看以下代码:
public static class FileFolderPermissions
{
public static void SetFileFolderPermissions()
{
try
{
DirectoryInfo dInfo = new DirectoryInfo(@"D:\SomeFolder");
DirectorySecurity dSecurity = dInfo.GetAccessControl();
FileInfo fInfo = new FileInfo(@"D:\Test.txt");
FileSecurity fSecurity = fInfo.GetAccessControl();
dSecurity.AddAccessRule(new FileSystemAccessRule("TestAccount",
FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit,
PropagationFlags.NoPropagateInherit, AccessControlType.Allow));
dInfo.SetAccessControl(dSecurity);
fSecurity.AddAccessRule(new FileSystemAccessRule("TestAccount",
FileSystemRights.FullControl, AccessControlType.Allow));
fInfo.SetAccessControl(fSecurity);
}
catch
{
Console.WriteLine("Error.");
}
}
}
我要做的是创建一种通用方法来设置对象是文件还是目录的 ACL。如您所见,上面的代码中有很多代码重复。所以我一直试图弄清楚如何将“DirectoryInfo”或“FileInfo”一般地传递给上面的代码,所以我没有所有的重复。
我曾经遇到能够将类型保存到变量中。但是我看到的大多数与 Activator 打交道的示例都涉及创建对象的实例。
如您所见,这不是创建实例。所以这就是为什么我想知道“如何概括 FileSecurity/DirectorySecurity fSecurity/dSecurity 部分”?
谢谢你的帮助。
解决方案:根据 nawfal 提供的答案,这里是更新的类,它现在适用于 FileInfo 和 DirectoryInfo 以及 Main 方法中的代码。注意:我注释掉了处理抛出异常的部分,因为我还没有实现异常。
public static class DynFileFolderPermissions
{
public static void SetFileFolderPermissions(dynamic info, FileSystemAccessRule FileAccessRule)
{
// if (!(info is System.IO.FileInfo) || !(info is System.IO.DirectoryInfo))
// throw new System.InvalidOperationException("Incorrect Type.");
try
{
var security = info.GetAccessControl();
security.AddAccessRule(FileAccessRule);
info.SetAccessControl(security);
}
catch
{
Console.WriteLine("Error.");
}
}
}
// Main
class Program
{
static void Main(string[] args)
{
// Grants FullControl to user "TestUser" on file D:\Test.txt
var fileInfo = new FileInfo(@"D:\Test.txt");
var FileAccessRule = new FileSystemAccessRule("TestUser", FileSystemRights.FullControl, AccessControlType.Allow);
DynFileFolderPermissions.SetFileFolderPermissions(fileInfo, FileAccessRule);
// Grants FullControl to user "TestUser" on directory D:\Test
var directoryInfo = new DirectoryInfo(@"D:\Test");
var DirectoryAccessRule = new FileSystemAccessRule("TestUser", FileSystemRights.FullControl,
InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit,
PropagationFlags.None, AccessControlType.Allow);
DynFileFolderPermissions.SetFileFolderPermissions(directoryInfo, DirectoryAccessRule);
}
}