readonly

How do I delete a directory with read-only files in C#?

放肆的年华 提交于 2019-11-26 06:06:27
问题 I need to delete a directory that contains read-only files. Which approach is better: Using DirectoryInfo.Delete() , or, ManagementObject.InvokeMethod(\"Delete\") ? With DirectoryInfo.Delete() , I have to manually turn off the read-only attribute for each file, but ManagementObject.InvokeMethod(\"Delete\") doesn\'t appear to need to. Is there any situation where one is more preferable to the other? Sample code (test.txt is read only). First way: DirectoryInfo dir = new DirectoryInfo(@\"C:\

Declare a const array

情到浓时终转凉″ 提交于 2019-11-26 06:04:44
问题 Is it possible to write something similar to the following? public const string[] Titles = { \"German\", \"Spanish\", \"Corrects\", \"Wrongs\" }; 回答1: Yes, but you need to declare it readonly instead of const : public static readonly string[] Titles = { "German", "Spanish", "Corrects", "Wrongs" }; The reason is that const can only be applied to a field whose value is known at compile-time. The array initializer you've shown is not a constant expression in C#, so it produces a compiler error.

Read/Write Python Closures

本秂侑毒 提交于 2019-11-26 05:52:41
问题 Closures are an incredibly useful language feature. They let us do clever things that would otherwise take a lot of code, and often enable us to write code that is more elegant and more clear. In Python 2.x, closures variable names cannot be rebound; that is, a function defined inside another lexical scope cannot do something like some_var = \'changed!\' for variables outside of its local scope. Can someone explain why that is? There have been situations in which I would like to create a

What are the benefits to marking a field as `readonly` in C#?

泪湿孤枕 提交于 2019-11-26 05:17:09
问题 What are the benefits of having a member variable declared as read only? Is it just protecting against someone changing during the lifecycle of the class or are there any compiler speed improvements due to this keyword 回答1: The readonly keyword is used to declare a member variable a constant, but allows the value to be calculated at runtime. This differs from a constant declared with the const modifier, which must have its value set at compile time. Using readonly you can set the value of the

OneWayToSource binding from readonly property in XAML

半世苍凉 提交于 2019-11-26 03:55:51
问题 I\'m trying to bind to a Readonly property with OneWayToSource as mode, but it seems this cannot be done in XAML: <controls:FlagThingy IsModified=\"{Binding FlagIsModified, ElementName=container, Mode=OneWayToSource}\" /> I get: The property \'FlagThingy.IsModified\' cannot be set because it does not have an accessible set accessor. IsModified is a readonly DependencyProperty on FlagThingy . I want to bind that value to the FlagIsModified property on the container. To be clear: FlagThingy

Why isn&#39;t String.Empty a constant?

血红的双手。 提交于 2019-11-26 02:08:38
问题 In .Net why is String.Empty read only instead of a constant? I\'m just wondering if anyone knows what the reasoning was behind that decision. 回答1: The reason that static readonly is used instead of const is due to use with unmanaged code, as indicated by Microsoft here in the Shared Source Common Language Infrastructure 2.0 Release. The file to look at is sscli20\clr\src\bcl\system\string.cs . The Empty constant holds the empty string value. We need to call the String constructor so that the

Can I change a private readonly field in C# using reflection?

元气小坏坏 提交于 2019-11-26 01:29:46
问题 I am wondering, since a lot of things can be done using reflection, can I change a private readonly field after the constructor completed its execution? (note: just curiosity) public class Foo { private readonly int bar; public Foo(int num) { bar = num; } public int GetBar() { return bar; } } Foo foo = new Foo(123); Console.WriteLine(foo.GetBar()); // display 123 // reflection code here... Console.WriteLine(foo.GetBar()); // display 456 回答1: You can: typeof(Foo) .GetField("bar",BindingFlags

Pushing read-only GUI properties back into ViewModel

≡放荡痞女 提交于 2019-11-26 00:23:50
问题 I want to write a ViewModel that always knows the current state of some read-only dependency properties from the View. Specifically, my GUI contains a FlowDocumentPageViewer, which displays one page at a time from a FlowDocument. FlowDocumentPageViewer exposes two read-only dependency properties called CanGoToPreviousPage and CanGoToNextPage. I want my ViewModel to always know the values of these two View properties. I figured I could do this with a OneWayToSource databinding:

In a Django form, how do I make a field readonly (or disabled) so that it cannot be edited?

懵懂的女人 提交于 2019-11-25 22:27:53
问题 In a Django form, how do I make a field read-only (or disabled)? When the form is being used to create a new entry, all fields should be enabled - but when the record is in update mode some fields need to be read-only. For example, when creating a new Item model, all fields must be editable, but while updating the record, is there a way to disable the sku field so that it is visible, but cannot be edited? class Item(models.Model): sku = models.CharField(max_length=50) description = models