c#-3.0

Mapping business Objects and Entity Object with reflection c#

ε祈祈猫儿з 提交于 2019-12-18 07:04:52
问题 I want to somehow map entity object to business object using reflection in c# - public class Category { public int CategoryID { get; set; } public string CategoryName { get; set; } } My Entity Class has same properties, CategoryId and CategoryName.Any best practice using reflection or dynamic would be appreciated. 回答1: You can use Automapper or Valueinjecter Edit: Ok I wrote a function that uses reflection, beware it is not gonna handle cases where mapped properties aren't exactly equal e.g

How to implement an inherited Dictionary over WCF

梦想的初衷 提交于 2019-12-18 04:17:40
问题 I’m trying to implement a dictionary for use with WCF. My requirements are: actual (private variable or base class) type equivalent to Dictionary Comparer = System.StringComparer.InvariantCultureIgnoreCase Custom (override/new) Add(key, value) method (to include validations). Override ToString() Use of the same type on both the client and host I’ve attempted using this class in a common project shared by the WCF host and client projects: [Serializable] public class MyDictionary : Dictionary

ASP:ListBox Get Selected Items - One Liner?

落爺英雄遲暮 提交于 2019-12-18 04:04:09
问题 I am trying to get the selected items of an asp:ListBox control and put them in a comma delimited string. There has got to be a simpler way of doing this then: foreach (ListItem listItem in lbAppGroup.Items) { if (listItem.Selected == true) { Trace.Warn("Selected Item", listItem.Value); } } Is there a way to get this into one line? like my pseudo code here: string values = myListBox.SelectedItems; I am using ASP.NET and C# 3.5. Thanks for any help!! 回答1: Using LINQ: string values = String

Argument type 'void' is not assignable to parameter type 'System.Action'

[亡魂溺海] 提交于 2019-12-18 03:59:09
问题 This is my test code: class PassingInActionStatement { static void Main(string[] args) { var dsufac = new DoSomethingUsefulForAChange(); dsufac.Do(WriteToConsole); dsufac.Do2(s => WriteToConsoleWithSomethingExtra("Test")); dsufac.Do(WriteToConsoleWithSomethingExtra("Test")); // Does not compile } internal static void WriteToConsole() { Console.WriteLine("Done"); } internal static void WriteToConsoleWithSomethingExtra(String input) { Console.WriteLine(input); } } internal class

C# Access denied to path in a Windows Application

爷,独闯天下 提交于 2019-12-18 03:41:58
问题 I've built a Windows Application using c#, on Windows 7. Everything was working fine, so I've created a Setup Wizard project and then built it. Once I install the app, I can open it correctly, but when I try to make some action that writes a text file(with logging purposes) it crashes, thrwoing me the following error message: UnauthorizedAccessException Access to the path 'C:\Program Files (x86)\MSProgram\MSProgram\log.txt' is denied. When I manually give that folder full rights, it works

object to string array

可紊 提交于 2019-12-17 22:39:11
问题 I am trying to convert an object (is declared here as 'obj': object is array, primitive) to a string array. object can be anything uint[], int16[], etc. I have been trying to use string[] str = Array.ConvertAll<object, string>((object[])obj, Convert.ToString); The problem occurs when I try to cast the unknown type object into object[]. I have been getting casting error. One attempt I made, which failed, was using object[] arr = (object[])obj; or IEnumerable<object> list = obj as IEnumerable

Outputting a Unicode character in C#

江枫思渺然 提交于 2019-12-17 20:24:35
问题 I'm new to programming and self taught. I'm trying to output the astrological symbol for Taurus, which is supposed to be U+2649 in Unicode. Here is the code I'm using... string myString = "\u2649"; byte[] unicode = System.Text.Encoding.Unicode.GetBytes(myString); Console.WriteLine(unicode.Length); The result I'm getting is the number 2 instead of the symbol or font. I'm sure I'm doing something wrong. 回答1: You need to have a font which displays that glyph. If you do, then: Console.WriteLine

Can you find an Active Directory User's Primary Group in C#?

你说的曾经没有我的故事 提交于 2019-12-17 19:31:04
问题 I am working on an application that manages user accounts in Active Directory. I am using the System.DirectoryServices.AccountManagement namespace whereever possible, but I can't figure out how to determine a user's primary group. When I try to remove a group that is the user's primary group I get an exception. Here is my current code: private void removeFromGroup(UserPrincipal userPrincipal, GroupPrincipal groupPrincipal) { TODO: Check to see if this Group is the user's primary group.

C#3.0 Automatic properties, why not access the field directly?

半城伤御伤魂 提交于 2019-12-17 19:15:43
问题 With the new approach of having the get/set within the attribut of the class like that : public string FirstName { get; set; } Why simply not simply put the attribute FirstName public without accessor? 回答1: Two of the big problems with direct access to variable inside class (field/attribute) are: 1) You can't easily databind against fields. 2) If you expose public fields from your classes you can't later change them to properties (for example: to add validation logic to the setters) 回答2:

Favorite way to create an new IEnumerable<T> sequence from a single value?

萝らか妹 提交于 2019-12-17 17:59:25
问题 I usually create a sequence from a single value using array syntax, like this: IEnumerable<string> sequence = new string[] { "abc" }; Or using a new List. I'd like to hear if anyone has a more expressive way to do the same thing. 回答1: Your example is not an empty sequence, it's a sequence with one element. To create an empty sequence of strings you can do var sequence = Enumerable.Empty<string>(); EDIT OP clarified they were looking to create a single value. In that case var sequence =