Is there any way to Add to child classes in an inherited Class? I have a huge class file that I've ran through an XSD to class file generator. I'd rather not have to manually edit the generated file each time an update is made to the XSD file. Is there any way I can inherit all members of the generated file and add to its child classes?
Example: If I wanted to add string Child_String2 how could I do this?
File1.cs
namespace test
{
class AutoGenerated
{
string Parent_string { get; set; }
public class Child
{
string child_string1 { get; set; }
//I want to add string Child_String2 {get;set;} at this level in to ModifiedParent from file "MyAttempt.cs"
}
}
}
MyAttempt.cs
namespace test
{
class ModifiedParent: Parent
{
Child.string child_string2{get;set;}
}
}
This obviously does not work, but the intention is to add child_string2 to the Child class inside of Parent.
Is there any way to accomplish something like this?
The original reason of this question was because I needed to serialize/deserialize to a class that I am adding to. If I were to inherit just from "Child", I am able to assign my modified "Child" inside of "Parent" but the serializer then has an issue as it does not expect child_string2.
Any tips, suggestions or even links to solving this myself would be appreciated.
Edit:
So after discussion with coworkers and searching around online it sounds like we haven't been able to come up with any direct solution. Unfortunately it sounds like the best approach would just be to edit the Generated CS file. I do have access to the generator so I decided best approach would be to add a few lines to the generator to make the appropriate changes each time it is generated.
Related
Is there a way to find references of code that updates objects of a class in VS 2015 with Resharper?
Example: let's say I have the following code:
public class A {
public int Prop1 {get; set;}
public void Update()
{
Prop1 = 100
}
public String ToString()
{
return Prop1.ToString();
}
}
some code:
A a = new A();
a.Prop1 = 100; // write access
a.Update(); // write access
Console.Writeln(a); // read-only access
I want a way of finding only those statements that modify the objects of a class A. In the code above that would be the lines where I have the comment // write access. Resharper can find all the usages of a class but there is too much noise from read-only usages such as linq queries, and I wanted to see only the updates. Reporting the constructor call is fine. Reporting only the statements that set the properties of the objects of class A would be fine as well. The classes that I want to find the updates for are EF entities and they have only a few methods or none.
Thanks
This isn't a Resharper solution. More of a quick-and-dirty search. Assuming that the class is within your solution and there aren't way too many properties, change the properties to read-only. The compiler errors will show you anything that writes to the properties.
ReSharper solution: you can find all usages of the particular property and then there are two separate buttons in results window to show read or write usages separately.
So, after some searching on stackoverflow and on google, I did find a few answers on my question but didn't know how to actually implement it for my own use. Which is why I will ask it again here.
Because of my inexperience with wpf combined with that I have never used anything closely resembling it, I made the mistake of going into creating a class diagram which now(maybe) has to be completely changed.
I wasn't sure of how I would go about creating custom blocks, but kind of mapped out the behaviour I needed.
close example of what I was trying to work towards
After realizing I made a mistake after finding out about userControls I tried implementing this into my project.
This is a test I made:
base class
namespace TestTest
{
public abstract partial class TestBase : UserControl
{
public TestBase()
{
InitializeComponent();
}
public virtual void doSomething()
{
Console.WriteLine("ITS WORKING");
}
}
}
Child class
namespace TestTest
{
public partial class TestExtend : TestBase
{
public TestExtend()
{
InitializeComponent();
}
}
}
after this I tried to add testExtended to a canvas but it gave the type error: testExtended is not a UIElement
class diagram (both UIStackBlock and UISideBlock inherit from UIBlock and UIBlock is free to inherit from anything)
I will still need the inheritance I created for filtering out certain blocks(mostly on UIOperations)
Now my real question is, how would I go about adding multiple xaml files and defining the look of them in there without getting in the way of the current inheritance hierarchy I have in place. Or is this even achievable?
Again, I know this is a duplicate.
You can either write DataTemplates that are adjusted per DataType.
Or, you could leave behind inheriting from UserControl(just Inherit from Control or ContentControl) and make each of them a custom control with its unique style and Template.
I'm currently using .Settings files to store level data (Level1.settings, Level2.settings) in a WinForms application. Please nevermind why I would do that :) I'm currently reading each level with a different function, because I can't figure out how to pass to a function.
In my application I have:
AppTitle
Properties
Level1.settings
Level2.settings
I am currently reading them with separate functions, for example:
void readLevel1()
{
levelMessage = Properties.Level1.Default.WelcomeMessage;
}
In this example, how could I pass "Level1" as a parameter to the function?
I would have preferred something like:
void readLevel(String identifier)
{
levelMessage = ConfigurationManager.AppSettings[identifier].Default.WelcomeMessage;
}
But I'm getting syntax errors on "Default" says, 'string does not contain a definition for 'Default' and not extension method 'Default' accepting a first argument of String can be found'. Any help would be greatly appreciated!
New Info:
OK I have been banging my head on the wall all day and here's what I came up with, that seems to work:
Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
ConfigurationSection cs = config.SectionGroups["applicationSettings"].Sections["AppTitle.Properties.Level1"];
ClientSettingsSection css = (ClientSettingsSection)cs;
String s = css.Settings.Get("WelcomeMessage").Value.ValueXml.InnerText;
If anyone knows a better way please let me know! Using InnerText feels like a hack.
Thank you Ann for trying to help!
I am going to assume that Level1 and Level2 have all the same properties with all the same definitions.
In that case, you could create an interface that both Level1 and Level2 would implement. You could then define your settings-reading method like this:
void ReadLevel(ILevelSettings settingsInstance)
{
levelMessage = settingsInstance.WelcomeMessage;
}
You would call it like this:
ReadLevel(Properties.Level1.Default);
If the generated classes for Level1 and Level2 are partial classes (and I can't remember if they are) this should be easy to implement.
ETA: Partial class demonstration:
The partial keyword on a class definition means that the definition of the class is not necessarily all within one source code file. You can have others.
So you can create your own source code file, call it something like "Level1.partial.cs", and use this code:
namespace AppTitle.Properties
{
internal sealed partial class Level1 : ILevelSettings
{
// any additional functions or properties that you would like
// your Level1 class to have.
}
}
(Disclaimer: that was from memory. I omitted the using statements.)
At compile time, the compiler will gather up all the definitions of class Level1 that have the same namespace and modifying keywords (internal, sealed) and build them into a single class, mixing the generated methods and properties and any ones you added.
You can thereby make the generated Level1 and Level2 classes inherit from an interface (or even a base class), even though most of the class definition was generated. You can regenerate it at any time without affecting the code you added, because it's in a different file.
My problem is similar to this one, except that the code I want to use with protobuf-net was generated by the WSDL utility. "They tell me" that it's not appropriate to plan to modify the generated code (to add attributes).
It would be odd to write something to build a matching set of DTO classes, given that (other than the methods described) all the classes are already public (partial) classes marked with [Serializable] and have all public properties -- that is, they are very DTO-like already.
The simplest solution I've seen is to violate the rules and modify the code. If all strings in the generated C# file
public partial class
are replaced with
[ProtoBuf.ProtoContract(ImplicitFields = ProtoBuf.ImplicitFields.AllPublic)]
public partial class
from my limited testing that will do the trick. Does a better solution exist? Is there something wrong with using that technique (other than that I'm modifying generated code)?
Type-level attributes in partial classes can be added in separate code files. All you need is a separate file with just:
[ProtoBuf.ProtoContract(ImplicitFields = ProtoBuf.ImplicitFields.AllPublic)]
public partial class {}
it must also be in the right namespace, but you didn't say which, so probably:
namespace Foo {
[ProtoBuf.ProtoContract(ImplicitFields = ProtoBuf.ImplicitFields.AllPublic)]
public partial class {}
}
I have a class which is has tons of properties. Most of them are of custom types. I want to get all those properties, type of whose interface is same.
Public class abc:IamLegend
{
few properties
}
public class def:IamLegend
{
few properties
}
public class on_which_iamworking
{
public abc propabc{ get; set; }
public def propdef{ get; set; }
public someothertype propother{ get; set; }
}
I want something which returns propabc and propdef.
I know how to do it using reflection, but I am looking for another way.
I am working on c# 4.0
Thanks
I am afraid that this is not possible at runtime without using reflection. That's what reflection is designed for.
The main problem of reflection is that it is slow. If you don't want to use reflection only because of it's slowness, you could make caching of your property list in some static property or class. I used this tecknique widely in similar problems and there wasn't any problems with perfomance.
If you have holy war against reflection, you could create a special util that parses C# file (or builds your prokects, loads output assembly and use reflection, but only before build, not in run-time), finds needed properties and writes it into autogenerated file (maybe also C# code file) as static-class array-property initializer. And call that util on pre-build event of your project. Then you'll get all needed properties completely without reflections =) (but I wouldn't do that)
Well, there's two ways:
1/
return new List<string> { "propabc", "propdev" };
2/ Reflection :P
If you need to retrieve the list of properties many times and are afraid of the performance impact, compute the list only once and store it in a static property (as the list of properties of a class won't change during runtime).
There is an alternative approach for components. It is TypeDescriptor for classes that implement IComponent. I believe that is used by WPF.