I understand how to assign a Type Class to a variable:
Type type = ("System.Security.AccessControl.FileSecurity").GetType();
How can I then use this variable as a reference for an already instantiated object?
If I try the following:
type instance = new SomeOtherType();
I get the following error code:
The type or namespace name 'type' could not be found (are you missing a using directive or an assembly reference?)
Example:
FileSecurity fSecurity = fInfo.GetAccessControl();
I would like to be able to do:
Type sometype = ("System.Security.AccessControl.FileSecurity").GetType();
sometype mynewtype = fInfo.GetAccessControl();
Edit: To better explain as to why I am even trying to do this in the first place, please take a look at this code:
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.");
}
}
}
What I am trying to do is create a generic way to set an ACL on whether the object is a File or Directory. There is a lot of code duplication in the above code as you can see. So I have been attempting to figure out how to generically pass either "DirectoryInfo" or "FileInfo" to the above code so I don't have all of the duplication.
I had come across being able to save a Type into a variable. But most of the examples I have seen dealing with Activator have involved creating an instance of the object.
As you can see, this is not creating an instance. So this is why I was wondering, "How to generalize the FileSecurity/DirectorySecurity fSecurity/dSecurity part"?
Thanks for your help.
SOLUTION: Based on the answer provided by nawfal, here is the updated class which now works for both FileInfo and DirectoryInfo along with the code in the Main method. Note: I commented out the section dealing with throwing an exception, as I haven't yet implemented the exception.
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);
}
}