How do I create an object from a type? I don't need to actually do anything
with the object. My code actually uses the object to get only type info
rather than anything specific about the object. (its because I'm passing
around objects instead of types)
Basically I use BinaryReader and pass an object to a function. I use that
object to figure out which binary reader method to use to read from the disk
Int32 x = 0;
Int32 y = Read(x);
x tells the function read that we are trying to read an Int32. Now sure I
could have passed a type and it would be easier in this case but in general
I'm passing around objects. In this one case I need to create a object from
a type because I am working with an array and I had to use GetElements to
get the element type.
If you must know, here is the code
// Creates a multi-dimensional array
int[] indices = new int[Rank];
Array a = Array.CreateInstance(type.GetType().GetElementType(), Lengths,
LowerBounds);
for(int i = 0; i < Length; i++)
{
// Sets up indices
indices[Rank - 1] = (i % Lengths[Rank - 1]);
for (int j = Rank - 2; j >= 0; j--)
indices[j] = (i / Lengths[j+1]) % Lengths[j];
Type t = type.GetType().GetElementType();
object o = ****************************
a.SetValue(Read(o), indices);
}
So if I do something like object o = "asdf"; and my array I'm working with
is of type string then everythign works.
But in general I need o to be of the same type as the elements of a(which is
of type.GetType().GetElementType()).
Again, the reason is because the read function uses o do determine what to
read. I might have to convert to using type by my code is pretty simple
using an object and if I use Type then I'll have to use .GetType just about
every time I call Read.
The Read function starts off like
public object Read(object type)
{
#region Primitives
if (type is SByte) { return br.ReadSByte(); }
if (type is Int16) { return br.ReadInt16(); }
and it makes using Read a bit easier later on. The problem is, is that it
seems its very easy to go from object to type but not vice versa. I've tried
Activator.CreateInstance but I get errors about the constructor.
When I do
Type t = type.GetType().GetElementType();
object o = Activator.CreateInstance(t);
I get the error "No parameterless constructor defined for this object" when
t is of type string. And for my app it doesn't matter at all what kinda
constructor is called with whatever type of parameters(for the most part).
I guess ultimately I'm just going to have to switch Read to work with types
;/
Jon Slaughter - 04 Apr 2008 05:10 GMT
I converted the Read function to use type and everything works fine but I'd
still like to know about what I asked.
Marc Gravell - 04 Apr 2008 05:20 GMT
Activator.CreateType(type) would be the simplest, but that requires a
default constructor (or rather: knowledge of which constructor you
want). Passing a Type is a more pragmatic approach.
Marc
Marc Gravell - 04 Apr 2008 05:21 GMT
> Activator.CreateType(type) would be the simplest
Early morning fingers... meant CreateInstance ;-p
And re-reading. it seems you've already found exactly the issue with
classes that don't have default constructors [such as string].
Marc