473,473 Members | 2,193 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Creating an instance of a no-constructor-struct with reflection

Hi all,

I would like to create an instance of a struct with
reflection.

The way I am used to doing this with classes:
- get a ConstructorInfo from the Type
- call Invoke()

However, if a struct does not have a constructor
declared, I don't get a single default constructor.
Here is a short code snippet that counts constructors:
using System;
using System.Reflection;

public class StructCreationWithReflection {

public static void Main(string[] args){
Type type = typeof(MyStruct);
ConstructorInfo[]cons = type.GetConstructors(
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance
);
Console.WriteLine(cons.Length);
}
}

public struct MyStruct{
public int foo;
}
Is there any other possibility to create a struct, if I
only have it's type during runtime?
If I add a constructor to a struct, there are no problems
and I can use reflection to create instances perfectly,
just the same as if I was using a class.
Has the reflection feature simply been forgotten for
no-constructor structs?
Thanks for any hints.
Kind regards,
Carl
--
Carl Rosenberger
db4o - database for objects - http://www.db4o.com
Nov 15 '05 #1
2 3531
Carl,
Is there any other possibility to create a struct, if I
only have it's type during runtime?


Activator.CreateInstance()

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/
Please reply only to the newsgroup.
Nov 15 '05 #2
Carl Rosenberger <ca**@db4o.com> wrote:
I would like to create an instance of a struct with
reflection.

The way I am used to doing this with classes:
- get a ConstructorInfo from the Type
- call Invoke()

However, if a struct does not have a constructor
declared, I don't get a single default constructor.


*All* structs have a parameterless constructor, implicitly. However, it
doesn't show up using reflection. You can create an instance with
Activator.CreateInstance though, and all the fields will be initialised
with their default values.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Nanda | last post by:
hi, I am trying to generate parameters for the updatecommand at runtime. this.oleDbDeleteCommand1.CommandText=cmdtext; this.oleDbDeleteCommand1.Connection =this.oleDbConnection1;...
2
by: Rajesh Tammana | last post by:
Hi, I am trying to create an SMS package using GetPDFData method. When I call this method it returns the PackageData object instance with C# object type ManagementBaseObject. Usually I call...
3
by: Seth | last post by:
I have been trying to create a mock httpcontext for the purpose of unit testing. I have a class, Customer, that uses cookies. I have set it up to be able to take a httpcontext as a parameter in...
12
by: Mats Lycken | last post by:
Hi, I'm creating a CMS that I would like to be plug-in based with different plugins handling different kinds of content. What I really want is to be able to load/unload plugins on the fly without...
17
by: Lee Harr | last post by:
I understand how to create a property like this: class RC(object): def _set_pwm(self, v): self._pwm01 = v % 256 def _get_pwm(self): return self._pwm01 pwm01 = property(_get_pwm, _set_pwm)
32
by: Joe | last post by:
I am just starting to use Object Oriented PHP coding, and I am seeing quite often the following (this example taken from a wiki): $wakka =& new Wakka($wakkaConfig); What exactly is the =&, and...
15
by: mark.norgate | last post by:
Hello I want to create a reference to an object, so that changes to the referenced object are reflected in the other object. Like this: object o = 123; object p = o; o = 456;
9
by: colin | last post by:
Hi, I have all the components of a .bmp file in memory wich i need to write to a .bmp file, (or other convenient image file) ie i have 256 color pallete of RGBquads and array of 8bit pixels, I...
19
by: =?Utf-8?B?WWFua2VlIEltcGVyaWFsaXN0IERvZw==?= | last post by:
I'm doing my c# more and more like i used to code c++, meaning i'm casting more often than creating an instance of objects. like : protected void gvOrderDetailsRowDataBound(object sender,...
3
by: Peter Morris | last post by:
Hi all Let's say I am creating a model to represent classes and properties. In addition to this I need instances of classes and values for those properties. KEY: (AssociationEndName)
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.