Get TypeInfo in static constructor - static

Is there any way to get the equivalent of GetType within a static constructor?
I want to iterate through the available properties of the type within the static constructor but GetType is an instance method? Why is this? The typeinfo should exist in the static context. Is there a way around this?

Just use
Type type = typeof(TheCurrentType);
It should never be more complex than this since you always know the actual type; there's no polymorphism to deal with in static methods.

I don't think you can get derived types (other than by iterating through all types to see what derives from the current type). To get the current type, you can:
Type currentType = (new StackFrame()).GetMethod().DeclaringType;

Related

How to create a GObject final class with both public and private members?

In the chapter Boilerplate code of GObject Manual, when ViewerFile is declared as a final type using G_DECLARE_FINAL_TYPE, how can we add public data to it since it is hidden behind the viewer-file.c which is not included?
The main distinction between a "derivable" GObject type and a "final" GObject type is the visibility of the instance data structure.
If the GObject type is "derivable" then you can only use a private instance data structure, as the instance structure is public and it's generated to just include the parent's structure.
If the GObject type is "final" then you only get instance fields, since the instance data structure is private to your C source file.
You cannot mix the two approaches, unless you decide not to use the macros and write the boilerplate by hand.
Additionally, you should not ever access fields on an instance data structure; provide accessor functions, instead, so that you can validate pre-conditions and post-conditions safely.

How can I populate an instance of an object with random values?

Is there any way I can give AutoFixture an instance of an object and have it go through all the setters and set random data? The wiki examples only show how to obtain an instance from AutoFixture, e.g.
var autoGeneratedClass = fixture.Create<ComplexParent>();
My example use case is a factory method which generate instances of objects with dynamic properties based on a configuration. I want to test that my methods correctly, detect and interact (e.g. copy) these dynamic properties.
dynamic dynamicPropertyObject1 = factoryMethod(configuration);
dynamic dynamicPropertyObject2 = factoryMethod(configuration);
dynamicPropertyObject1.propA = random.Next();
dynamicPropertyObject1.CopyTo(dynamicPropertyObject2);
Assert.That(dynamicPropertyObject2.propA, Is.EqualTo(dynamicPropertyObject1.propA);
Thanks
AutoFixture has a lot of built-in heuristics for creating objects, including some for factory methods.
If AutoFixture finds no public constructors on a type, it starts to look for factory methods; i.e. static methods that return objects of the type of the class that defines that static method, e.g.
public class Foo
{
public static Foo CreateFoo();
// ... other members
}
If, on the other hand, a factory method exists on another class, you'll need to help AutoFixture a bit. The easiest way would be to use the Customize method:
fixture.Customize<Foo>(c => c
.FromFactory(() => FooFactory.CreateFoo())
.WithAutoProperties());
When you subsequently ask a Fixture object for a Foo object, FooFactory.CreateFoo() will be invoked, and because of WithAutoProperties that object will be populated with data created by AutoFixture.

CObject, CStringArray and error C2248

class MyClass : public CObject
{
public:
MyClass();
private:
CStringArray m_myArray;
};
causes error c2248
What's wrong with this ?
I think it's related to the fact that CStringArray and MyClass both derive from CObject.
Legacy class derived from CObject currently uses CustomArray it just seems wrong to me so I would like to replace it by CStringArray.
Microsoft compiler error C2248 means "Members of a derived class cannot access private members of a base class."
I can only assume you're trying to directly reference MyClass::m_myArray from somewhere in your code, without using a public accessor function.
Update
The real answer is that the problem is caused by the copy-constructor for MyClass, attempting to copy m_myArray, but it can't, because CObject derived classes aren't copyable by default. The solution would be to write a copy constructor yourself, and rather than attempt to copy the array, copy the contents one at a time, from the source array to the destination array. Either that or use std::vector (which you should be doing anyway, as MFC containers are horrible).
You are trying to access some private member of CObject. Since both your MyClass and CStringArray derive from CObject, without more context it is impossible to know the exact problem.
The only idea that comes to mind, basically because I've fallen in the trap many times, is that CObject's copy constructor is private, so if you are trying to copy either the CStringArray or your own class, explicit or implicitly, you'll get the error.
UPDATE:
I've just taken a look at CObject's declaration and the assignment operator, too, is private. Everything else is either public or protected.
Error C2248 says "Members of a derived class cannot access private members of a base class."
CStringArray class dosn't expose Copy constructor and assignment operator, look in code for such places and replace the code with elemenet by elemnet copy.

int[] type and documentation

I'm puzzled about arrays in C#.
I can't find any documentation on MSDN about for example the object double[].
I do find documentation about int, array, collections, ... but can't find out of what type double[] is. If I do double[] a = new double[10]; a.GetType(), I find that the type of a is System.Double[]
I believe that the type is not System.Double[], but must be some descendent of an object or interface in the System.Collections namespace or the System.Collections.Generic namespace. My best guess is that a double[] is a type that implements the interface System.Collections.IList, but I'm not sure at all.
So, could anyone explain me what kind of object an array of doubles (so an object double[]) really is? What are it's base classes, which interfaces does it implement, ...
Where could I find documentation on arrays of objects defined as int[], ..., object[] ?
Thanks!
Array is a base class for all types of arrays int, double, string whatever, the base class for System.Array is System.Object. so the correct type is System.Double[]. It implements the ICloneable,
IList, ICollection, IEnumerable, IStructuralComparable, IStructuralEquatable interfaces as stated on msdn.
The main difference between arrays and collection is that arrays cannot be changed in size at runtime and arrays have to contain objects of same type as opposed to collections.

private static method problem

When a class is defined as a private static, why do I need to make the get and set methods static?
Because you can't return a static member from an instance method.
It seems redundant to have to mark all members in a static class as static but C# requires that you do this. It's just the way the compiler was implemented.
As far as I know there are no members that inherit any modifiers from the type by default. In other words, a public class's members are not all public by default, etc. By requiring that you mark each member as static you are explicitly laying out the contract of the type.

Resources