473,671 Members | 2,121 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Property parameter?

Is there a way to pass a property around as a parameter without reflection?

My current motivation for this is the code below is testing multiple
properties to see that an exception is thrown if a user tries to set the
string value to an empty value. Ideally I'd like to have one method that
would take a Property as a parameter and test it:

[Test]
public void CompanyName_Err or_CannotSetToE mpty() {
var customer = new Customer { ID = "TEST" };
try {
customer.Compan yName = string.Empty;
Assert.Fail("Em ptyValueExcepti on for CompanyName property should
have been thrown.");
}
catch (EmptyValueExce ption ex) {
_assertProperty NameIsSetProper ly(ex, "CompanyNam e");
}
}

[Test]
public void PhoneNumber_Err or_CannotSetToE mpty() {
var customer = new Customer { ID = "TEST" };
try {
customer.Phone = string.Empty;
Assert.Fail("Em ptyValueExcepti on for Phone property should have
been thrown.");
}
catch (EmptyValueExce ption ex) {
_assertProperty NameIsSetProper ly(ex, "Phone");
}
}

private static void _assertProperty NameIsSetProper ly(EmptyValueEx ception
ex, string expectedName) {
Assert.That(ex. PropertyName, Is.EqualTo(expe ctedName));
}
Sep 30 '08 #1
5 2014
On Sep 30, 2:36*pm, "Berryl Hesh" <efing...@yahoo .comwrote:
Is there a way to pass a property around as a parameter without reflection?
Not really - although you could pass round a delegate or pair of
delegates to get/set the property. That would be quite readable using
anonymous methods or lambda expressions.

Jon
Sep 30 '08 #2
can you please show me what that would look like?

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:97******** *************** ***********@8g2 000hse.googlegr oups.com...
On Sep 30, 2:36 pm, "Berryl Hesh" <efing...@yahoo .comwrote:
Is there a way to pass a property around as a parameter without
reflection?
Not really - although you could pass round a delegate or pair of
delegates to get/set the property. That would be quite readable using
anonymous methods or lambda expressions.

Jon
Sep 30 '08 #3
On Sep 30, 2:45*pm, "Berryl Hesh" <efing...@yahoo .comwrote:
can you please show me what that would look like?
Which version of C# are you using?

Jon
Sep 30 '08 #4
3.5

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:37******** *************** ***********@m44 g2000hsc.google groups.com...
On Sep 30, 2:45 pm, "Berryl Hesh" <efing...@yahoo .comwrote:
can you please show me what that would look like?
Which version of C# are you using?

Jon
Sep 30 '08 #5
On Sep 30, 3:02*pm, "Berryl Hesh" <efing...@yahoo .comwrote:
3.5
Okay, I'll assume you mean you're using .NET 3.5 and C# 3.0. In which
case you can use lambda expressions:

public void SomeMethod<T>(F unc<Customer,Tg etter, Action<Customer ,T>
setter) {
Customer customer = ...;
// When you need to test the getter:
AssertEquals("" , getter(customer )); // Or whatever

// When you need to test the setter
setter(customer , value);
}

Then to call SomeMethod:

SomeMethod(cust omer =customer.Phone , (customer, value) =>
customer.Phone = value);

Jon
Sep 30 '08 #6

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

Similar topics

2
1945
by: Aaron | last post by:
Hi, I've seen javascript code where a constructor function is passed an argument "document", and inside the function itself the assignment "this.document = document;" is made. This is the code (or the part necessary for the example): function ToggleButton(document) { ToggleButton.images = new Array(4); for(i=0;i<4;i++) { ToggleButton.images = new
1
1704
by: Heather | last post by:
How is the Parameter property for menu items used? Thanks! Heather
4
2481
by: Roberto Sartori | last post by:
Hi. I'd want to know if it is possible in C # to declare one property analogous to following (written in VB): Property PropertyName (ByVal Index As Integer) As Object Get Return List.Item(Index) End Get Set (ByVal Value As Object)
1
8425
by: Andy G | last post by:
I've been getting this error all day. Could someone please look at my stored procedure and the code. I have a form that grabs and email address the user types in, calls a stored procedure with an input parameter and an output parameter, compared the email to a table and matches an ID in another table to grab the Username. thanks for any helps guys/girls! Andy :)
6
9057
by: Cc | last post by:
hi, is there a way to use byref on property set , because i would like to pass the value into the variable byref ?
4
4033
by: Pritcham | last post by:
Hi all I've got a number of classes already developed (basic entity classes) like the following: Public Class Contact Private _firstname as String Private _age as Integer Public Property FirstName As String
1
1312
by: yeltsin27 | last post by:
I can see how to use a cookie, control property, form input value, profile, query string or session value as a parameter to a SQL Data Source. However I would like to use a property that is declared on the page (which as it happens is persised in VIEWSTATE). I know I could write the property to a control, but that's no different to writing the value to the ControlParameter, which is what I'm doing now.
0
6983
by: Bryce Fischer | last post by:
I've got a simple (I think) asp.net application. I've created a DataSet in App_Code/ItemDataSet.xsd. Tested connection, seemed to work fine. In my ASPX file, I first dropped an ObjectDataSource onto the form, and pointed it to the dataset created above. I then dropped a GridView on the form, and selected the ObjectDataSource I created above. In the Design view it seems to be at least loading the
3
1961
by: Peter Gast | last post by:
Hi, I need as a parameter for a control the names of my properties as a string. How can I get the name of the property as a string during runtime Example: Private _MyVar As Double Public ReadOnly Property MyVar As Double Get Return _MyVar End Get
7
1363
by: Andy B | last post by:
I saw this in the set accessor of a property: Set(ByVal value As DataSet) What exactly does the stuff in the () mean? VS complained about it not being there when I took it out not knowing it needed to be there.
0
8393
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,...
0
8914
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8820
jinu1996
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...
1
8598
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,...
0
7433
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, 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...
0
5695
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();...
0
4224
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...
1
2810
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
2
1809
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.