Hi, I am trying to dynamically populate an objects properties using
reflection.
Specifically, I have an object called the theObj and it has a number
of properties.
property is a PropertyInfo object
What I want to do is to be able to set the property value dynamically
which I am doing so using this line of code (Simplified version below)
property.SetVal ue(theObj, TextBox1.Text, null);
The above works fine if the property is of type String as the item
TextBox1.Text returns a string.
If however, the property is of type Int32, DateTime etc it fails...
What I need to do is to cast the value returned from the TextBox.Text
(string) to the data type the property expects..
something like this
(This does not work)
property.SetVal ue(theObj, ((property.GetT ype())TextBox1. Text), null);
Any help appreciated
Thanks
Markus 3 4123
"MarkusJ_NZ " <Ma*******@gmai l.comwrote in message
news:11******** **************@ v29g2000prd.goo glegroups.com.. .
something like this
(This does not work)
property.SetVal ue(theObj, ((property.GetT ype())TextBox1. Text), null);
IConvertible ic = (IConvertible)T extBox1.Text;
Type targetType = property.Proper tyType;
property.SetVal ue(theObj, (ic.ToType(targ etType,
CultureInfo.Cur rentCulture)), null);
On Nov 6, 5:21 pm, "Alberto Poblacion" <earthling-
quitaestoparaco ntes...@poblaci on.orgwrote:
"MarkusJ_NZ " <Markus...@gmai l.comwrote in message
news:11******** **************@ v29g2000prd.goo glegroups.com.. .
something like this
(This does not work)
property.SetVal ue(theObj, ((property.GetT ype())TextBox1. Text), null);
IConvertible ic = (IConvertible)T extBox1.Text;
Type targetType = property.Proper tyType;
property.SetVal ue(theObj, (ic.ToType(targ etType,
CultureInfo.Cur rentCulture)), null);
Thanks :)
For completeness - there is also something called a TypeConverter that
is accsesible from a PropertyDescrip tor (which is very similar in
concept to a PropertyInfo); this allows for a much more context-
specific conversion, as you can specify the converter against
individual properties rather than the type. However, in most simple
cases a straight ToType will suffice.
Marc This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Sunny |
last post by:
Hi,
I have an old problem which I couldn't solve so far. Now I have found a post
in that group that gave me an idea, but I can not fully understand it.
The problem is: I'm trying to use a Windows.Forms.UserControl in a COM
environment, i.e. I want to host that control in a COM host. So far, so
good, I can host it, but I can not reach the parent COM object from the
control (Parent property is null :( ).
I have stopped the control in the...
|
by: Jeff |
last post by:
I am trying to dynamically load an assembly via reflection and then invoke a
method of that assembly that will populate a custom type collection passed
into the method byref. I am able to dynamically load both the DALC
component (for the method call) and the Entity component (for the custom
type collection to pass in), but I keep getting an error ( Message "Object
type cannot be converted to target type.") when invoking the method. If I...
|
by: HL |
last post by:
The requirement is to send some information to other objects. The objects to
whom the information has to be sent is not available at compile time. The
names of the types (objects) will be provided through an external file in
which case we can use reflection to create objects of that type at run time.
Case 1:
The methods of those types can be invoked through reflection.
Case 2:
The caller can publish certain events. The types (objects)...
|
by: liko81 |
last post by:
I have an Invoice class that must know, directly or indirectly, how to
do anything associated with creating, reading, or otherwise processing
an invoice to a customer. It is an uber-DAO object that combines the
business logic and DAO layers. The invoice it handles can have one to
many lines. The most logical implementation is an Invoice object with
an array of InvoiceLine objects. We of course want to prevent Joe
Programmer from directly...
|
by: heddy |
last post by:
I understand that reflection allows me to discover the metadata of a
class at runtime (properties, methods etc). What I don't understand is
where this is useful. For example:
If I am the sole author of an application, I already know the metadata
about my classes - I wrote them. And even if I did not - Let's say I
buy a 3rd party class lib, it will be documented for me with that lib.
If I am part of a dev team, the same is true - If...
| |
by: George |
last post by:
Hello,
I am building an assembly that connects to a third party application via
http. I need create a http message that I post to the third party
application. The message is very complicated with alot of business rules
which I can define in a xml/config file. So, I will need to parse the xml
file and also use reflection to retrieve certain data from my business
objects via properties. This assembly will be used alot so performance is...
|
by: jerryau |
last post by:
Hi,
I am trying to dynamically create object instances based on a string
class name, and then I need to dynamically set values to these
objects, but I have no idea how to do this in C#. Here is the example
of what I need to do:
******Dog.cs******
namespace MySystem
{
|
by: raylopez99 |
last post by:
What good is C# Reflection, other than to find out what types are in
an assembly? And to dynamically invoke methods in an assembly (.dll
or .exe)?
Also, bonus question, can you use Reflection to build a compiler? One
that will construct a user defined class "on the fly" (literally, the
user defines a class, instantiates it, and runs it from the console
mode, all the while prompted by the program)?
I guess so, but my final question...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
| |
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |