Automatic creation of constructors in C# - c#

I'm just wondering if there's an automated way to generate constructors with every possible combination of the parameters you might need.
I have a ctor with 4 parameters, but I want to provide overloads where a developer could pass in a single param, or two or three. By hand I've been writing every possible combination and passing defaults to the 4 parameter one. I also need to then introduce two more "full" prototypes ( with a fifth ), then create all the possible combinations for those as well, so I need loads of ctor overloads to cover all combinations.
I'd like to manually write the three full ctors, then be able to generate the combinations with a context menu click. I haven't seen an option like this in Resharper. Anyone know if there's an existing solution out there already?

If you need a lot of constructor parameters, rather than struggling with the explosion of possible permutations, consider creating an "options" class that has sensible defaults:
public class FooOptions
{
public FooOptions()
{
MaintenanceInterval = TimeSpan.FromSeconds(30);
MaximumIdleTime = TimeSpan.FromMinutes(5);
}
public TimeSpan MaintenanceInterval { get; set; }
public TimeSpan MaximumIdleTime { get; set; }
//etc...
}
then
class Foo
{
public Foo():this(new FooOptions())
{
}
public Foo(FooOptions opts)
{
//...
}
}

This situation would be a perfect fit for the Builder pattern.
For example, if the class Foo can have any combination of a String, an int and a Bar:
public class Foo
{
public string MyString { get; set; }
public int MyInt { get; set; }
public Bar MyBar { get; set; }
}
Instead of adding a constructor with every possibility, make a Builder. Here's an example of a simple fluent implementation:
public class FooBuilder
{
private Foo foo = new Foo();
public FooBuilder WithString(String someString)
{
foo.MyString = someString;
return this;
}
public FooBuilder WithInt(int someInt)
{
foo.MyInt = someInt;
return this;
}
public FooBuilder WithBar(Bar someBar)
{
foo.MyBar = someBar;
return this;
}
public Foo Build()
{
return foo;
}
}
which can be used like this:
Foo foo = new FooBuilder().WithString("abc").WithInt(3).Build();
This eliminates completely the need for an exponential number of constructors.

Don't need multiple constructor overloads - try using optional/default parameters. Relevant link: https://msdn.microsoft.com/en-us/library/dd264739.aspx
Example code:
class Program
{
static void Main(string[] args)
{
var defaultMonster = new Monster();
var archerMonster = new Monster("crossbow");
var knightMonster = new Monster("broadsword", "plate mail");
var wizardMonster = new Monster(armor: "wizard robe", magicItem: "wand");
Console.WriteLine(defaultMonster);
Console.WriteLine(archerMonster);
Console.WriteLine(knightMonster);
Console.WriteLine(wizardMonster);
}
}
class Monster
{
private readonly string _weapon;
private readonly string _armor;
private readonly string _magicItem;
public Monster(string weapon = "scimitar", string armor = "leather", string magicItem = "nothing")
{
_weapon = weapon;
_armor = armor;
_magicItem = magicItem;
}
public override string ToString()
{
return string.Format("Monster armed with {0}, wearing {1}, carrying {2}", _weapon, _armor, _magicItem);
}
}

Related

Generate a strongly-typed proxy that can track changes on property names not values when one property is set to another

Setup:
public class Data
{
public int A { get; set; }
public int B { get; set; }
}
public class Runner
{
public static void Run(Data data)
{
data.A = data.B;
data.A = 1;
}
}
class Program
{
static void Main(string[] args)
{
var data = new Data() { A = 1, B = 2 };
Runner.Run(data);
}
}
Problem: I need to implement change tracking here for property names not values. Inside Runner.Run on the first line data.A = data.B I need to record somehow that "A" was set to "B" (literally property names) and then on the next line data.A = 1 I need to record that "A" was set to constant and say forget about it.
Constrains:
When setting one property to another (e.g. A = B) that needs to be recorded
When setting property to anything else (e.g. A = 1 or A = B * 2) this change needs to be forgotten (e.g. remember A only)
Suppose this is the tracker contract being used:
void RecordChange(string setterName, string getterName);
void UnTrackChange(string setterName);
Question:
I would like to somehow proxy the Data class so it still can be used in the interface code (e.g. Runner - is a whole bunch of a business logic code that uses Data) INCLUDING strong-typing and it can track it's changes without modifying the code (e.g. there is lots of places like 'data.A = data.B').
Is there any way to do it without resorting to I guess some magic involving IL generation?
Already investigated/tried:
PostSharp interceptors/Castle.DynamicProxy with interceptors - these alone cannot help. The most I can get out of it is to have a value of data.B inside setter interceptor but not nameof(data.B).
Compiler services - haven't found anything suitable here - getting the name of caller doesn't really help.
Runtine code generation - smth like proxy inherited from DynamicObject or using Relfection.Emit (TypeBuilder probably) - I lose typings.
Current solution:
Use the Tracker implementation of the abovementioned contract and pass it around into every function down the road. Then instead of writing data.A = data.B use method tracker.SetFrom(x => x.A, x => x.B) - tracker holds a Data instance and so this works. BUT in a real codebase it is easy to miss something and it just makes it way less readable.
It is the closest the solution I've come up with. It isn't perfect as I still need to modify all the contracts/methods in the client code to use a new data model but at least all the logic stays the same.
So I'm open for other answers.
Here's the renewed Data model:
public readonly struct NamedProperty<TValue>
{
public NamedProperty(string name, TValue value)
{
Name = name;
Value = value;
}
public string Name { get; }
public TValue Value { get; }
public static implicit operator TValue (NamedProperty<TValue> obj)
=> obj.Value;
public static implicit operator NamedProperty<TValue>(TValue value)
=> new NamedProperty<TValue>(null, value);
}
public interface ISelfTracker<T>
where T : class, ISelfTracker<T>
{
Tracker<T> Tracker { get; set; }
}
public class NamedData : ISelfTracker<NamedData>
{
public virtual NamedProperty<int> A { get; set; }
public virtual NamedProperty<int> B { get; set; }
public Tracker<NamedData> Tracker { get; set; }
}
Basically I've copy-pasted the original Data model but changed all its properties to be aware of their names.
Then the tracker itself:
public class Tracker<T>
where T : class, ISelfTracker<T>
{
public T Instance { get; }
public T Proxy { get; }
public Tracker(T instance)
{
Instance = instance;
Proxy = new ProxyGenerator().CreateClassProxyWithTarget<T>(Instance, new TrackingNamedProxyInterceptor<T>(this));
Proxy.Tracker = this;
}
public void RecordChange(string setterName, string getterName)
{
}
public void UnTrackChange(string setterName)
{
}
}
The interceptor for Castle.DynamicProxy:
public class TrackingNamedProxyInterceptor<T> : IInterceptor
where T : class, ISelfTracker<T>
{
private const string SetterPrefix = "set_";
private const string GetterPrefix = "get_";
private readonly Tracker<T> _tracker;
public TrackingNamedProxyInterceptor(Tracker<T> proxy)
{
_tracker = proxy;
}
public void Intercept(IInvocation invocation)
{
if (IsSetMethod(invocation.Method))
{
string propertyName = GetPropertyName(invocation.Method);
dynamic value = invocation.Arguments[0];
var propertyType = value.GetType();
if (IsOfGenericType(propertyType, typeof(NamedProperty<>)))
{
if (value.Name == null)
{
_tracker.UnTrackChange(propertyName);
}
else
{
_tracker.RecordChange(propertyName, value.Name);
}
var args = new[] { propertyName, value.Value };
invocation.Arguments[0] = Activator.CreateInstance(propertyType, args);
}
}
invocation.Proceed();
}
private string GetPropertyName(MethodInfo method)
=> method.Name.Replace(SetterPrefix, string.Empty).Replace(GetterPrefix, string.Empty);
private bool IsSetMethod(MethodInfo method)
=> method.IsSpecialName && method.Name.StartsWith(SetterPrefix);
private bool IsOfGenericType(Type type, Type openGenericType)
=> type.IsGenericType && type.GetGenericTypeDefinition() == openGenericType;
}
And the modified entry point:
static void Main(string[] args)
{
var data = new Data() { A = 1, B = 2 };
NamedData namedData = Map(data);
var proxy = new Tracker<NamedData>(namedData).Proxy;
Runner.Run(proxy);
Console.ReadLine();
}
The Map() function actually maps Data to NamedData filling in property names.

C# Class Function Return Class (Performance)

I have created a class like this.
public class SimpleClass
{
public string myProp { get; set; }
public SimpleClass()
{
this.myProp = "";
}
public SimpleClass Method1()
{
this.myProp += "Method1";
return this;
}
public SimpleClass Method2()
{
this.myProp += "Method2";
return this;
}
public string GetProp()
{
return this.myProp;
}
}
I'm using it like this.
public class Worker
{
public Worker()
{
string Output = new SimpleClass().Method1().Method2().GetProp();
}
}
All functions return the container class and last method returns result.
I'm curious about this performance, is it bad thing to use methods like for performance or good?
Should i use it like that or could you suggesst another way.
Thanks
some suggestion :
how should user know First Call method1 then Method2 and finally GetProp()?
It's better to encapsulate your methods and hide all Complexity. For example User just call GetProp() and in GetProp() you can Do what you want .
your exmple can change like below :
public class SimpleClass
{
public string myProp { get; set; }
public SimpleClass()
{
this.myProp = "";
}
private string Method1()
{
this.myProp += "Method1";
return Method2();
}
private string Method2()
{
return this.myProp += "Method2";
}
public string GetProp()
{
Method1();
return this.myProp;
}
}
Finally call your Prop() Method like :
SimpleClass simple = new SimpleClass();
string Output = simple.GetProp();
And Another suggestion to have better Design is Make Your Mathod1 and Method2 as Private.
I think that you are reinventing the wheel in wrong way. you are probably looking for StringBuilder which does exactly same thing.
var builder = new StringBuilder();
var result = builder.Append("something").Append("something else").ToString();
but if you still want to have dedicated class to provide meaningful methods instead of just Append and also provide some abstraction over arguments being passed you can do this.
public class SimpleClass
{
private readonly StringBuilder _builder = new StringBuilder();
public SimpleClass Method1()
{
_builder.Append("Method1");
return this;
}
public SimpleClass Method2()
{
_builder.Append("Method2");
return this;
}
public string GetProp()
{
return _builder.ToString();
}
}
Note that using StringBuilder is efficient way of appending strings together. for small number of appends it may not show difference, but for large number of appends it will be faster and produces less garbage.

Mapping an interface to concrete class

I have this weird behavior of AutoMapper. If I have a class that implements two interfaces were both of them are registered in CreateMap only one of them gets mapped and I do not understand why. It looks as if AutoMapper mapped only interface that is mentioned at first place in interfaces list.
Any thoughts? Please, checkout the following fiddler to see what the problem is.
Code from fiddler
A DTO to which I'd like to map:
class Dto {
public string StringValue {get;set;}
public int Value {get;set;}
}
Interfaces - sources from which I'd like to map:
interface ISourceA {
int Value {get;}
}
interface ISourceB {
string StringValue {get;}
}
...and two implementations for those. One that works, the other not really :/
class MultiSource: ISourceA, ISourceB {
private readonly string _s;
private readonly int _v;
public MultiSource(int v, string s) {
_v = v;
_s = s;
}
int ISourceA.Value { get { return _v; }}
string ISourceB.StringValue { get { return _s; }}
}
class StringSource: ISourceB {
public StringSource(string value) {
StringValue = value;
}
public string StringValue {get; private set;}
}
Here's how I use it:
public class Program
{
public static void Main()
{
Mapper.CreateMap<ISourceA, Dto>();
Mapper.CreateMap<ISourceB, Dto>();
var ms = new MultiSource(234, "woefjweofij");
var ss = new StringSource("iuahergiuw");
// This one is fine
Console.WriteLine(JsonConvert.SerializeObject(Mapper.Map<Dto>((ISourceA)ms)));
// This one is the same as that above. This is not what I intended to have in return :/
Console.WriteLine(JsonConvert.SerializeObject(Mapper.Map<Dto>((ISourceB)ms)));
// This works as expected
Console.WriteLine(JsonConvert.SerializeObject(Mapper.Map<Dto>(ss)));
}
}

C# extended interface implementation as a parameter to delegate that takes base interface

I have an interface that extends some other interface, like this:
interface IBase
{
int Id { get; set; }
string Name { get; set; }
}
interface IExtended : IBase
{
bool IsChecked { get; set; }
}
Then I use base interface as a parameter in a delegate function that is also a parameter to class constructor, like this:
public class SomeClass
{
private IBase _model;
private Func<IBase, string> _handler;
public SomeClass(IBase model, Func<IBase, string> handler)
{
_model = model;
_handler = handler;
}
public string ExecuteHandler()
{
return _handler(model);
}
}
Interface implementations:
public class BaseImplementation : IBase
{
int Id { get; set; }
string Name { get; set; }
public BaseImplementation(int id, string name)
{
Id = id;
Name = name;
}
}
public class ExtendedImplementation : IExtended
{
int Id { get; set; }
string Name { get; set; }
bool IsChecked { get; set; }
public BaseImplementation(int id, string name, bool isChecked)
{
Id = id;
Name = name;
IsChecked = isChecked;
}
}
Intended use:
BaseImplemetation baseModel = new BaseImplementation(1, "base");
ExtendedImplemetation extendedModel = new ExtendedImplementation(2, "extended", true);
SomeClass someClass1 = new SomeClass(baseModel, (IBase arg) => {
Console.Write("Remember, " + arg.name + ", YOLO!");
});
SomeClass someClass2 = new SomeClass(extendedModel, (IExtended arg) => {
Console.Write(arg.name + ", YOLO! You're " + (arg.IsChecked) ? "checked!" : "not checked!");
});
string res1 = someClass1.ExecuteHandler();
string res2 = someClass2.ExecuteHandler();
But that ( doesn't work, even though implementation of IExtended would necessarily implement everything that is defined by IBase interface. Why is that so and how would I bypass this and get the result I want?
EDIT:
I think I got it now.
I thought that Func<IBase, string> is equal to Func<IExtended, string> because IExtended of course implements everything that IBase does, so there should be no problem, right? Implementation as I wanted it to be and is listed in my example would of course work just fine.
BUT! The problem is that someClass2 can't be constructed like that because, as #Servy mentioned, delegate function could do something like this:
SomeClass someClassWrong = new SomeClass(baseModel, (IExtended arg) => {
if (arg.IsChecked) {
// gotcha, baseModel doesn't have IsChecked property!
}
});
EDIT 2:
Thank you everybody for you help and sorry for constant editing and giving wrong example sof what I want :D
But that doesn't work, even though implementation of IExtended would necessarily implement everything that is defined by IBase interface. Why is that so and how would I bypass this and get the result I want?
When SomeClass invokes that delegate it might not actually pass an IExtended instance. It's allowed to provide any IBase instance as the parameter, so if it provides one that doesn't implement IExtended, then what would you expect your delegate to do?
If SomeClass is always going to pass an IExtended instance, then modify the delegate it accepts in its constructor accordingly, so that the callers always know they're getting an IExtended instance as a parameter.
You can simply define a delegate that knows the IBase is really an IExtended:
SomeClass someClass = new SomeClass((IBase arg) => { (arg as IExtended).DoSomethingOnlyExtendedKnowsAbout(); });
This is potentially unsafe, but if you somehow can enforce that the arg passed to that specific lamda will always be an IExtended then there is no harm. You could also provide a safety mechanism in the lambda itself and manage it accordingly up the call stack:
SomeClass someClass = new SomeClass((IBase arg) => { (arg as IExtended)?.DoSomethingOnlyExtendedKnowsAbout(); });
I don't see the problem. Based on the code you have, the following works as intended:
public class SomeClass
{
public SomeClass(Func<IBase, string> handlerFcn)
{
// something gets done
this.Handler=handlerFcn;
}
public Func<IBase, string> Handler { get; set; }
}
public static class Program
{
static void Main(string[] args)
{
var s1 = new SomeClass((x) => x.SomeMethod());
var xt = new ExtendedClass();
var result = s1.Handler(xt);
// result = "yolo extended edition!"
}
}
I think you were trying to use the concrete class ExtendedClass in the lambda definition and that won't work unless you define it as a closure.

Inherit in generic classes C#

My brain is gonna to explode. :) So I would like to get help from you.
Please, think about my question like about just programmer puzzle. (Actually. perhaps it is very easy question for you, but not for me.)
It is needed to create array of objects. For example List where T is class. (I will describe Class T below). Also it is needed create “container” that will contain this array and some methods for work with this array. For example Add(), Remove(int IndexToRemove).
Class T must have field "Container", this way each elements of our array would be able to know where is it contained and has access its container's fields and methods. Notice, that in this case Class T should have type parameter. Indeed, it is not known beforehand which container's type is used.
Let us denote this class container as A and class element (class T) as AUnit.
Code:
class Program
{
static void Main(string[] args)
{
A a = new A();
a.Add();
a.Units[0].SomeField +=100;
Console.ReadKey();
}
}
class A
{
public List<AUnit> Units;
public A()//ctor
{
Units = new List<AUnit>();
}
public void Add()
{
this.Units.Add(new AUnit(this));
}
}
class AUnit
{
public int SomeField;
public A Container;
public string Name { get; private set; }
public AUnit(A container)
{
this.SomeField = 43;
this.Container = container;
this.Name = "Default";
}
}
Public fields should be protected or private of course, but let think about this later.
You can ask “why we create public A Container field in AUnit”? We create field public string Name{get;private set;} (actually property but nevermind). And also we would like to be able to change value of this field for example method [Class AUnit] public bool Rename(string newName)();. The main idea of this method is changing Name field only that case if no one element in array (public List Units; ) has the same name like newName. But to achieve this, Rename method has to have access to all names that is currently used. And that is why we need Container field.
Code of extended version AUnit
class AUnit
{
public int SomeField;
public A Container;
public string Name { get; private set; }
public AUnit(A container)
{
this.SomeField = 43;
this.Container = container;
this.Name = "Default";
}
public bool Rename(String newName)
{
Boolean res = true;
foreach (AUnit unt in this.Container.Units)
{
if (unt.Name == newName)
{
res = false;
break;
}
}
if (res) this.Name = String.Copy(newName);
return res;
}
}
Ok. If you still read it let's continue. Now we need to create Class B and class BUnit which will be very similar like Class A and Class Aunit. And finally the main question of this puzzle is HOW WE CAN DO IT? Of course, I can CopyPaste and bit modify A and AUnit and create this code.
class B
{
public List<BUnit> Units; //Only Type Changing
public B()//ctor Name changing...
{
Units = new List<BUnit>();//Only Type Changing
}
public void Add()
{
this.Units.Add(new BUnit(this));//Only Type Changing
}
}
class BUnit
{
public int SomeField;
public B Container;//Only Type Changing
public string Name { get; private set; }
public A a; //NEW FIELD IS ADDED (just one)
public BUnit(B container) //Ctor Name and arguments type changing
{
this.SomeField = 43;
this.Container = container;
this.Name = "Default";
this.a=new A(); //New ROW (just one)
}
public bool Rename(String newName)
{
Boolean res = true;
foreach (BUnit unt in this.Container.Units) //Only Type Changing
{
if (unt.Name == newName)
{
res = false;
break;
}
}
if (res) this.Name = String.Copy(newName);
return res;
}
}
And I can to use this classes this way.
static void Main(string[] args)
{
B b = new B();
b.Add();
b.Units[0].a.Add();
b.Units[0].a.Units[0].SomeField += 100;
bool res= b.Units[0].a.Units[0].Rename("1");
res = b.Units[0].a.Units[0].Rename("1");
Console.ReadKey();
}
This construction is can be used to create “non-homogeneous trees”.
Help, I need somebody help, just no anybody…. [The Beatles]
I created B and BUnit using CopyPaste.
But how it can be done using “macro-definitions” or “Generic”, inherit or anything else in elegant style? (C# language)
I think that there is no reason to describe all my unsuccessful attempts and subquestions. Already topic is too long. : )
Thanks a lot if you still read it and understand what I would like to ask.
You need to implement a base type, lets call it UnitBase, with all common functionality. I'd structure your code the following way:
Create an interface for your container, this way you can change implementation to more performant solutions without modifying the elements you will be adding to the container.
public interface IContainer
{
Q Add<Q>() where Q : UnitBase, new();
IEnumerable<UnitBase> Units { get; }
}
Following the idea stated in 1, why not make the search logic belong to the container? It makes much more sense, as it will mostly depend on how the container is implemented:
public interface IContainer
{
Q Add<Q>() where Q : UnitBase, new();
IEnumerable<UnitBase> Units { get; }
bool Contains(string name);
}
A specific implementation of IContainer could be the following:
public class Container : IContainer
{
public Container()
{
list = new List<UnitBase>();
}
private List<UnitBase> list;
public Q Add<Q>() where Q: UnitBase, new()
{
var newItem = Activator.CreateInstance<Q>();
newItem.SetContainer(this);
list.Add(newItem);
return newItem;
}
public IEnumerable<UnitBase> Units => list.Select(i => i);
public bool Contains(string name) =>
Units.Any(unit => unit.Name == name);
}
Create a base class for your AUnit and BUnit types condensing all common functionality:
public abstract class UnitBase
{
protected UnitBase()
{
}
public IContainer Container { get; private set; }
public int SomeField;
public string Name { get; private set; }
public void SetContainer(IContainer container)
{
Container = container;
}
public bool Rename(String newName)
{
if (Container.Contains(newName))
return false;
this.Name = newName; //No need to use String.Copy
return true;
}
}
Implement your concrete types:
public class BUnit : UnitBase
{
public int SpecificBProperty { get; private set; }
public BUnit()
{
}
}
Shortcomings of this approach? Well, the container must be of type <UnitBase>, I've removed the generic type because it really wasn't doing much in this particular case as it would be invariant in the generic type.
Also, keep in mind that nothing in the type system avoids the following:
myContainer.Add<BUnit>();
myContainer.Add<AUnit>();
If having two different types in the same container is not an option then this whole set up kind of crumbles down. This issue was present in the previous solution too so its not something new, I simply forgot to point it out.
InBetween , I am very thankful to you for your advices. Actually I can't say that I understood your answer in full, but using your ideas I have done what I want.
Looks like my variant works well. However I would like to hear your (and everyone) opinions about code described below. The main goal of this structure is creating non-homogeneous trees. So could you estimate it from this side.
First of all. We need to create interfaces for both classes. We describe there all "cross-used" functions.
public interface IUnit<T>
{
string Name { get;}
void SetContainer(T t);
bool Rename(String newName);
}
public interface IContainer
{
bool IsNameBusy(String newName);
int Count { get; }
}
Next. Create Base for Unit Classes for future inheritance. We will use in this inheritors methods from Container Base so we need generic properties and IUnit interface.
class UnitBase<T> : IUnit<T> where T : IContainer
Unfortunately I don't know yet how to solve the problem with Constructor parameters. That is why I use method
SetContainer(T container).
Code:UnitBase
class UnitBase<T> : IUnit<T> where T : IContainer
{
protected T Container;
public string Name { get; private set; }
public UnitBase()
{
this.Name = "Default";
}
public void SetContainer(T container)
{
this.Container = container;
}
public bool Rename(String newName)
{
bool res = Container.IsNameBusy(newName);
if (!res) this.Name = String.Copy(newName);
return !res;
}
}
Next. Create ContainerBase
ContainerBase should:
1) has IContainer interface.
2)has information about what it will contain:
... where U : IUnit<C>, new()
3)and .... has information about what itself is. This information we need to pass as parameter to SetContainer() method.
Code ContainerBase:
class ContainerBase<U, C> : IContainer //U - Unit Class. C-Container Class
where U : IUnit<C>, new()
where C : ContainerBase<U, C>
{
protected List<U> Units;
public U this[int index] { get { return Units[index]; } }
public ContainerBase()//ctor
{
this.Units = new List<U>();
}
public void Add()
{
this.Units.Add(new U());
this.Units.Last().SetContainer(((C)this));//may be a bit strange but actualy this will have the same type as <C>
}
public bool IsNameBusy(String newName)
{
bool res = false;
foreach (var unt in this.Units)
{
if (unt.Name == newName)
{
res = true;
break;
}
}
return res;
}
public int Count { get { return this.Units.Count; } }
}
Cast ((TContainer)(this)) may be is a bit strange. But using ContainerBase we always should use NewInheritorContainer. So this cast is just do nothing…looks like...
Finally. This classes can be used like in this example.
class SheetContainer : ContainerBase<SheetUnit,SheetContainer> {public SheetContainer(){}}
class SheetUnit : UnitBase<SheetContainer>
{
public CellContainer Cells;
public PictureContainer Pictures;
public SheetUnit()
{
this.Cells = new CellContainer();
this.Pictures = new PictureContainer();
}
}
class CellContainer : ContainerBase<CellUnit, CellContainer> { public CellContainer() { } }
class CellUnit : UnitBase<CellContainer>
{
public string ValuePr;//Private Field
private const string ValuePrDefault = "Default";
public string Value//Property for Value
{
//All below are Just For Example.
get
{
return this.ValuePr;
}
set
{
if (String.IsNullOrEmpty(value))
{
this.ValuePr = ValuePrDefault;
}
else
{
this.ValuePr = String.Copy(value);
}
}
}
public CellUnit()
{
this.ValuePr = ValuePrDefault;
}
}
class PictureContainer : ContainerBase<PictureUnit, PictureContainer> { public PictureContainer() { } }
class PictureUnit : UnitBase<PictureContainer>
{
public int[,] Pixels{get;private set;}
public PictureUnit()
{
this.Pixels=new int[,]{{10,20,30},{11,12,13}};
}
public int GetSizeX()
{
return this.Pixels.GetLength(1);
}
public int GetSizeY()
{
return this.Pixels.GetLength(0);
}
public bool LoadFromFile(string path)
{
return false;
}
}
static void Main(string[] args)
{
SheetContainer Sheets = new SheetContainer();
Sheets.Add();
Sheets.Add();
Sheets.Add();
Sheets[0].Pictures.Add();
Sheets[1].Cells.Add();
Sheets[2].Pictures.Add();
Sheets[2].Cells.Add();
Sheets[2].Cells[0].Value = "FirstTest";
bool res= Sheets[0].Rename("First");//res=true
res=Sheets[2].Rename("First");//res =false
int res2 = Sheets.Count;
res2 = Sheets[2].Pictures[0].Pixels[1, 2];//13
res2 = Sheets[2].Pictures.Count;//1
res2 = Sheets[1].Pictures.Count;//0
res2 = Sheets[0].Pictures[0].GetSizeX();//3
Console.ReadKey();
}
Looks like it works like I want. But I didn’t test it full.
Let me say Thank you again, InBetween.

Categories