So I am currently working on an extension and I want to change the Build Action of a specific ProjectItem to None.
What I tried
I noticed that the ProjectItem had a Properties property which contains the following KeyValuePair wher the key is BuildAction. Therefor I tried to set its Value to prjBuildActionNone which should be the correct value for it.
Anyway, when I hit Play and the code runs and I set a breakpoint on this very line:
prop.Value = "prjBuildActionNone"
The Debugger will never return to the line below and that is it.
Is there anything I am doing wrong with this approach or is this the totally wrong direction and the Properties property is for read-only purposes?
The value should be a member of the prjBuildAction enumeration, e.g. prjBuildAction.prjBuildActionNone.
Execution is not reaching the next line because the incorrect type is causing an exception.
Related
When I remove a user control property that I have created and used in a project. There will be as many errors as times I have used control in project. For example imagine I have user control and it has a property named p1. when I remove p1 I get errors saying myusercontrol.p1 doesn't exist.
Is there a way to get rid of them automatically?
If you want to remove a property, go ahead and do so - and fix all errors you find after that.
You could use ReSharper's Safe Delete, but that'll do little if you actually used the property:
var foo = someClass.PropertyToRemove;
bar.CallMethod(someClass.PropertyToRemove);
The code was there for a reason, and ReSharper cannot figure out for you what that code should become if you remove someClass.PropertyToRemove.
I wanted to know the difference between these 3 Settings.Default.<PropertyName> , Settings.Default.Properties and Settings.Default.PropertyValues .
I have a wpf window that would dynamically generate controls based on these Settings and then one can update the Settings values
I first used Settings.Default.Properties Collection , but I believe it does not update the values in either config or the physical settings file in the user folder.
So I used reflection to update , But I still couldn't figure how to obtain
values by Reflection . (May still need to research on this)
Settings.Default.GetType().GetProperty(propertyName,
typeof(string)).SetValue(source, fileDialog.FileName, null);
Settings.Default.Save();
Settings.Default.Reload();
Then I saw Settings.Default.PropertyValues has the latest updated values and tested that in debug mode,
string properyValue =
Convert.ToString(Settings.Default.PropertyValues[propertyName].PropertyValue);
strangely they don't seem to be working when I created the installer and exe . Still to figure what exactly wrong.
Can someone point me out if I am complicating things and missing something?
Update 1
After nflash's comment , I checked when the file was created. The file was not created when the application starts for all 3 , I even called Settings.Default.Save right at the start but it doesn't create the file Settings.Default.<PropertyName> , Settings.Default.Properties are instantiated but Settings.Default.PropertyValues not.
Only once I make a change in the Settings and Save , the file is created.
Update2
Right now the solution that I came with is
source.GetType().GetProperty(setting.Name, typeof(string))
.SetValue(source, "NewValue", null);
As mentioned by nflash , it would be type safe (Although Reflection has it's demirits) . But the Settings.Default.<PropertyName> is synchronized and instantiated correctly hence.
Just want to add that you can only change settings with "User" scope. When you save the new value of the setting the value is not saved in the config in the application path but instead it is saved in the user.config inside the %localappdata% folder (%localappdata%\CompanyName\ApplicationName_someGUID\AppVersion)
Update:
About your last update, the user.config file is only created when you save a setting with a value different from the default value of the setting.
I'm not sure if you still have questions about this, so I'm trying to add more info:
Settings.Default.<PropertyName> as wonko79 pointed out is just a property accessor to the corresponding value. If you look at the code behind the settings (or just Go To Definition of the property) you will see something like this:
public string PropertyName {
get {
return ((string)(this["PropertyName"]));
}
set {
this["PropertyName"] = value;
}
}
The array operator is accessing the underlying structure that holds the values that in fact is the PropertyValues.
The difference between the Properties and PropertyValues is a little bit trickier. These are really two different structures (one is a SettingsPropertyCollection and the other is a SettingsPropertyValueCollection). The Properties property is defined in the ApplicationSettingsBase class and the PropertyValues is defined in the SettingsBase class.
The SettingsProperty class (elements of the SettingsPropertyCollection) contains information about the setting itself (metadata?) and its default value.
The SettingsPropertyValue class (elements of the SettingsPropertyValueCollection) contains the actual value of the setting and some additional control information, like if the value is dirty, if it is using default value, etc.
In the end of the day this is all how .NET internally manages the Settings and all that we need to know is how to get and set these settings.
I always like to work with the properties that the setting designer generates as it is strongly typed and already makes the cast to the corresponding type. Also using the Properties or PropertyValues requires a string as a parameter and in case of a typo I will only get an error in runtime as opposed to the compile error that I get if misspell the name of the property.
To save the settings after changing them you have to call
Settings.Default.Save();
Settings.Default.<PropertyName> is a property accessor to the corresponding settings value.
Settings.Default.Properties is a collection of all settings in your settings file.
Settings.Default.PropertyValues is a collection of all values of the settings in your settings file.
Maybe Using Settings in C# is a good starting point to read.
The article mentioned by user1064248 is good info, but does not address the PropertyValues issue. I cannot add anything to the good advice from Rameez and nflash except for this pragmatic advice: If you wish to force PropertyValues to populate, you need only to force a value to change. In my settings I have a DateTime property called "Timestamp" and if you put this in the form Load event, you will find that nn1 is zero, and nn2 contains the count all of the properties:
int nn1 = Properties.Settings.Default.PropertyValues.Count;
Properties.Settings.Default.Timestamp = DateTime.UtcNow; // Forces PropertyValues to load
int nn2 = Properties.Settings.Default.PropertyValues.Count;
Recently I have faced with a very strange problem. My Action method must return JsonResult,and all is good untill the last break point before return (At this moment I have correct json result).Then in browser console I see error 500 (Internal server error).Nothing exception in debugger.When I start to chek every step in debugger with F10,F11 I have noticed something strange.Unexpected infinitive invokes to my model properties (sometimes to model properties,sometimes infinitive invoking functions and then model proerties).I decide that this infinitive loop provoked error (but I still misunderstanding why I couldn't see it in debugger - perhaps this is aspect of IIS debugging).Code hasnt got weak places (I dont show it because it will take much more than few space).I know that my question is not constructive in stackoverflow terminalogy but I hope that somebody has encountered the same problem.I need only ideas.Thanks.
SOLUTION
As noticed #mreyeros and # LastCoder self referencing can be the reason of such behavior.I have cheked my model in details and found this place:
private IEnumerable<CollegeEstimateModel> _initialModels;
public IEnumerable<CollegeEstimateModel> InitialModels
{
get { return _initialModels = _initialModels ?? CreateInitialModelsList(); }
}
where CollegeEstimateModel contains above properties
I have added [ScriptIgnore] attribute and all become ok.
You should start by checking to see if the model that you are trying to serialize to your JSON result does not contain a property with a self referencing property. For example you have an Order object that contains a collection of details. The detail record has a navigation property back up to the parent order, thus causing a loop during serialization of the order object. This is just a guess of course, but hope that it helps
To make this clear, I'm using .net 3.5
I'm currently creating an Visual Studio Visualizer in order to be able to see the values of individual rules in a WF RuleSet. Everything is ok, but now I need to do the following.
Let's say I have a rule which is "this.Age > 50" and one then action as MessageBox.Show("Bigger than 50") and one else action as MessageBox.Show("Less than 50")
I need to be able to select the rule "this.Age > 50" and evaluate it as true or false. Or I need to select part of the rule, eg. "this.Age" and evaluate it to let's say 70.
This last bit is working. I'm getting the object in which the RuleSet executes and getting the fileds and try to find one field named Age and if I do find, I get it's value, on the event of not finding the field I then look at properties. This is working.
The problem is evaluating "this.Age > 50". Of course, I could do a parser in this case and evaluate it, but the problem is then I would have to evaluate "this.Age > 50", "this.Age < 50"... I guess you understand the problem. And of course I also want to evaluate for example, "this.GetCurrentAge()", in which GetCurrentAge() is a method in the class represented by the object in which context the RuleSet executes.
I've thought I could try to inject a method at runtime into the object I currently have and which is instantiated. This way, I could create something as simples as
public string EvaluationMethod()
{
return (this.Age > 50).ToString();
}
in this case, I would at runtime build this method and inject it in the current instance of object.
The problem is that I can't seem to find a way to inject code into the current instance of the object. I can only find examples on how to create new classes at runtime, but nothing about only creating a method and inject it.
Can someone please help, or give any other idea?
Thanks
I have a descendant of DataTable that has the DebuggerDisplay attribute defined. The default visualizer for DataTable is removed when I add the DebuggerDisplay attribute. If I remove the DebuggerDisplay attribute, the DataTable visualizer returns. I want the default DataTable visualizer and my override for DebuggerDisplay.
Do you guys know how to get it working?
//does not work
//[DebuggerVisualizer("Microsoft.VisualStudio.Debugger.DataSetVisualizer", typeof(DataTable))]
//DebuggerDisplay attribute removes DataTable visualizer. Odd behavior to me.
[DebuggerDisplay("{TableName}, Count = {Rows.Count}, {GetColumnNames()}")]
public class MyTable<T> : DataTable where T : class{}
Just to clarify, I've got no idea why deriving and specifying a different attribute disables the visualizer.
I've tried something similar and nothing stops you from having both DebuggerDisplay and DebuggerVisualizer applied to a type. The image below shows both, the left circle is the debugger visualizer, the right circle is the debugger display:
However, you may get issues with trying to use the DataSetVisualizer type on your class. It took a lot of jiggery-pokery, but I ended up with the following definition for my class:
[Serializable]
[DebuggerVisualizer(typeof(EnhancedDataSetVisualizer.DataSetVisualizer),
typeof(EnhancedDataSetVisualizer.DataSetVisualizerSource))]
[DebuggerDisplay("{Name}")]
public sealed class SpecFlowTableWrapper : DataSet
{
// Body omitted, not important.
}
I was constantly having to change what arguments I specified in DebuggerVisualizer. It turns out the missing piece for me was specifying the VisualizerObjectSource.
I then get the debugger display showing my data set name, and when I click the magnifying glass it loads the DataSetVisualizer.
The important part in all this is two references:
Microsoft.VisualStudio.Debugger.DataSetVisualizer
This contains the DataSetVisualizer and DataSetVisualizerSource types.
Microsoft.VisualStudio.DebuggerVisualizers
This is a dependency of the other reference.
The second item is generally available in the "Add References..." dialog in Visual Studio, however the first item is found in the VS installation directory.
For me (VS version may vary):
C:\Program Files (x86)\Microsoft Visual Studio
10.0\Common7\Packages\Debugger\Visualizers\
Called:
Microsoft.VisualStudio.Debugger.DataSetVisualizer.dll
Make sure "Copy Local" is true for the first reference as well (it should be true by default anyway). Note that for debugging, this reference is now a dependency so you need to make sure it is in the working directory of whatever project you are debugging, otherwise you get VS debugger errors.
Re-build, start debugger, enjoy. Sorry it was 2 years late.