How can i create a partial class at runtime? - c#

How can i create a partial class at runtime?
Example: I have a class
public partial class A
{
}
I want to create partial of my A class at runtime from an XML file.
because I don't compile my project when any change in code.
I know this is little meaning less but i need this.
XOML files work for me?

The point of partial classes is to be able to define parts of it in different source files. In the end, after compilation, what you have is a regular class, indistinguishable of any other. So there is not such a thing as "instantiating a partial class".

The partial keyword is used to separate a class across multiple code files. The compiler merges them together into a single class.
MSDN: Partial Class Definitions

You can't.
A partial class doesn't exist in the CLR. It's more of a Visual Vtudio trick than anything else.

Related

How to extend System.Windows.Input.Keyboard class

I would like to add some additional methods to System.Windows.Input.Keyboard.I have tried to create a partial class like this;
namespace System.Windows.Input
{
public static partial class Keyboard
{
//some code...
}
}
However when I try this Resharper informs me that this is not a partial file and when I try to use it I get an ambiguous reference error although both Keyboards are shown as being in the same namespace. Is what I'm trying to do even possible and if not, why not?
No, it's not possible. Keyboard is a static class.
Static classes cannot be instantiated and they cannot be extended.
You can always write your own (static) class and put your methods in there.
You shouldn't use the same namespace as Keyboard, the class should be only static, you can name the class whatever you want "KeyboardExtensions" for exemple
See this for extension methods https://msdn.microsoft.com/en-us/library/bb383977.aspx
What you are trying to do is not possible because the Keyboard class is not partial to begin with.
The ambiguous reference is caused by the fact that you're creating a class that already exists in the same namespace so the compiler doesn't know which one to use.
You cannot create a static class that inherits from another static class as suggested by #harmoniemand. Static classes can only inherit from Object.
And you cannot create extension methods on a static class as #Hamza_L seems to be suggesting because you can't use static types as parameters in extension methods.
with a look at the manual, you could see that the System.Windows.Input.Keyboard is not defined as partial (look here) so you are not able to extend it this way.
The better way to do it, is to write an inherited class.
namespace MyApplication.Wrapper
{
public static class MyKeyboard : System.Windows.Input.Keyboard
{
//some code...
}
}

Design view of Form class that inherits from BaseForm<T> [duplicate]

I am trying to clean up all designer errors in our solutions and ran into the following error:
The designer could not be shown for this file because none of the classes within it can be designed. The designer inspected the following classes in the file: DoubleAttributeTextBoxBase --- The base class 'NumericAttributeTextBoxBase' could not be loaded. Ensure the assembly has been referenced and that all projects have been built.
The classes are both defined in the same assembly so I know it's not a reference problem. I'm wondering if it has anything to do with the fact that the base class is generic. Any ideas?
public class DoubleAttributeTextBoxBase : NumericAttributeTextBoxBase<double>
public class NumericAttributeTextBoxBase<T> : TextBox where T : IComparable, IComparable<T>
The base class for a class being designed must be non-abstract and non-generic. To make a class that inherits from a generic class designable. The workaround is to insert a trivial non-generic class in-between:
public partial class DoubleAttributeTextBoxBase
: NumericAttributeTextBoxBaseOfDouble
{
public DoubleAttributeTextBoxBase()
{
InitializeComponent();
}
// Now DoubleAttributeTextBoxBase is designable.
}
public class NumericAttributeTextBoxBaseOfDouble
: NumericAttributeTextBoxBase<double>
{
}
To make this as simple as possible, you can even put the non-generic class in the same file as the class you want to design. Just make sure to put it after the class (as I have done above) because the designer expects the first class in the file to be the one being designed.
I don't know of a solution, this has been a severe limitation of Visual studio since C# 2.0 came out. The only thing I can say is to add that control to the page at runtime, then at least you can have your designer back for everything else.
from msdn:
Your component or control cannot be a generic type, which is also called a template type or a parameterized type. The design environment does not support generic types.
http://msdn.microsoft.com/en-us/library/ms171843.aspx

Is it possible to declare a partial class in two projects

Consider we create a partial class in Project1 and we have a Project2 that has reference to Project1 .How is it possible to declare some other method of partial class in Project2 ?
thanks
The partial construct is only a compiler functionality, to allow a class to be spread out in several source files. The compiled class still lives in one and only one class library (dll file).
There are two ways to extend a class in another library:
Inheritance, unless the class is sealed. This requires that the calling code handles all object instantiation to instantiate the new derived class.
Extension methods, which makes the code look like there are new methods on that class, but that is just syntactic sugar. It won't change the class itself.
It is not possible to extend a partial class in another project. Partial is only compiler sugar. The compiler will create only one class in the resulting assembly.
You can use the Extension methods that allow you to create additional methods for existing class
Partial classes cannot exist out side assembly boundaries!

Multiple partial classes with a single namespace returning error

I've declared a partial class with name "BusinessLayer" in BL namespace, but now I want to add another class with same name, and with keyword "partial" in that namespace. But Whenever I try to add class with with name "BusinessLayer", I'm getting an error, saying I can't declared multiple classes with same name in a namespace. Then how can I use partial classes in a single namespace?
I would fix your code if you'd write some, even just as an example.
Anyway, you should tag both as "partial".
Any declaration of this class, anywhere, should have the "partial" keyword in order to allow it to compile properly.
All declarations of the class must have the partial keyword, otherwise they will be interpreted as different classes and being in the same namespace will cause a compile time error.
My best guess is you're missing a partial keyword somewhere.
Edit: By the way, partial classes can only be declared in the same assembly, iirc. So all parts of the partial class need to be in the same assembly.

WPF, share methods between two UserControl classes

I have two UserControl classes, RequiredFields and OptionalFields. I wanted to have a base class, Fields, that would inherit from UserControl and contain methods that both RequiredFields and OptionalFields would use. However, when I try to do that, I get the following error:
Partial declarations of 'MyNamespace.OptionalFields' must not specify different base classes
Here are my class definitions:
public partial class OptionalFields : Fields
public partial class RequiredFields : Fields
public abstract class Fields : UserControl
Even if I make Fields a partial class instead of abstract, or if I make it a regular non-abstract non-partial class, I get the same error.
Is what I'm wanting to do possible/reasonable? If not, what is the best way of sharing methods between UserControl instances?
Do you have an OptionalFields.xaml? If so, it is automatically generating OptionalFields.g.cs which contains the C# code that the XAML represents. It contains a class that inherits from UserControl (or whatever the XAML's root element is).
Try changing the root element in the XAML file to Fields.
Do you need to updated the other side of the partial? I.e. is OptionalFields.Designer.cs inheriting from UserControl still?
Edit: sorry being too winforms, I of course mean OptionalFields.xaml

Categories