Tag:

在部分类中设置属性的属性

我有一个由Entity Framework (EF)生成的员工类。 public partial class employee { private string name; public string Name { get{return name;} set{ name = value;} } } 现在我想在name属性中放置一个必需的属性,用于在我编写的另一个员工部分类中进行MVC3validation,以扩展由EF生成的那个,这样我就不必重写我的代码了如果我刷新EF生成的模型。 我写的部分类在同一个程序集和名称空间中。 public partial class employee { // What should I write here to add required attribute in the Name property? }

从单独的类库访问web.config?

我正在寻找一种实现以下目标的好方法: 我有一个Web应用程序(MVC 3),它有一个单独的类库,其中包含我正在制作的CMS的后端逻辑。 此CMS使用NHibernate连接到数据库。 我希望用户能够在他们的web.config文件中配置连接字符串(最终甚至是数据库的味道)。 我正在寻找的是从web.config文件获取连接字符串的好方法,即使DLL是完全独立的。 这可能吗? 我是否必须以某种方式将我的连接字符串传递给我的类库? 或者我可以在应用程序运行时访问它吗? 如果我必须在我的Web应用程序中创建一些代码以将连接字符串传递给我的类库,我怎样才能使这段代码尽可能便携,所以我不必再为我的下一个webapp写它? 非常感谢您的任何想法。

我可以访问内部类中的外部类对象

我有三个这样的课程。 class A { public class innerB { //Do something } public class innerC { //trying to access objB here directly or indirectly over here. //I dont have to create an object of innerB, but to access the object created by A //ie innerB objInnerB = objB; //not like this innerB objInnerB= new innerB(); } […]

为什么在实现接口时我不能使用兼容的具体类型

我希望能够做到这样的事情: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test { public interface IFoo { IEnumerable integers { get; set; } } public class Bar : IFoo { public List integers { get; set; } } } 为什么编译器抱怨..? Error 2 ‘Test.Bar’ does not implement interface member ‘Test.IFoo.integers’. ‘Test.Bar.integers’ cannot implement ‘Test.IFoo.integers’ because it does […]

将datarow提取到c#对象

我有一个类Item表示列表中的项目。 我有函数调用返回datatable的存储过程,我需要将数据表转换为项目数组。 这是我做的: public class Item { private string _ItemIdDataName = “item_id”; private string _ItemNameDataName = “item_name”; private string _PriceDataName = “price”; public long ItemId { get; set; } public string ItemName { get; set; } public float Price { get; set; } private Item(DataRow row) { if (row != null) { ItemId = long.Parse(row[_ItemIdDataName].ToString()); […]

为什么要使用类级访问修饰符而不是对象级?

在使用C#时,我最近意识到我可以从Foo的静态函数调用Foo对象的私有函数,甚至可以从其他Foo对象调用。 在我了解了访问修饰符的所有内容后,这对我来说听起来很奇怪。 据我所知,当你做一些属于某种内部过程的事情时,你会把一个函数设为私有。 只有对象本身知道何时使用这些函数,因为其他对象不应该/不能控制对象的流。 是否有任何理由为什么同一类别的其他对象应该从这个非常简单的规则中排除? 根据要求,一个例子: public class AClass { private void doSomething() { /* Do something here */ } public void aFunction() { AClass f = new AClass(); f.doSomething(); // I would have expected this line to cause an access error. } }

C#,struct vs class,更快?

可能重复: 哪个最适合数据存储结构/类? 考虑一个示例,其中我有一个Employee对象,其中包含年龄,姓名,性别,职称,薪水等属性。 我现在有一个列表,我想填充一堆Employees(每个Employee实例是唯一的)。 就速度和内存占用而言,将员工创建为Struct还是Class更为可取? 关于上述场景中Struct和Class的任何其他警告都是受欢迎的

WriteLine与一个类

我正在制作一个学习C#的SchoolApp程序,我有这个主要function,我正在努力工作: namespace SchoolApp { class Program { public static void Main(string[] args) { School sch = new School(“Local School”); sch.AddStudent(new Student { Name = “James”, Age = 22 }); // this function sch.AddStudent(new Student { Name = “Jane”, Age = 23 }); Console.WriteLine(sch); // this implementation } } } 学校class级: namespace SchoolApp { class School […]

在c#中用字符串变量初始化一个类?

是否可以通过字符串变量初始化类? 我有PHP代码。 我怎么在c#中这样做?

是否有任何工具用随机数据填充类属性?

我想要做的是创建一个具有不同属性的属性的类,将该类传递给另一个将使用适当的随机数据设置属性的类…这里是伪代码: public class Customer { [Attribute(“FirstName”)] private string CustomerFirstName; public {get;set} //etc [Attribute(“LastName”)] private string CustomerLastName; public {get;set;} //etc [Attribute(“DateTime”)] private DateTime CustomerSignUpDate; public DateTime {get;set;} //yadda [Attribute(“Phone”)] private string CustomerPhone; public string {get;set;} //yadda } 然后这样做 IList CustomerList=ClassFillerOutClass(new Customer(),5); 结果将是5个客户的列表,其属性中具有适当的“随机”数据。 如果这不存在……我想我可以自己开始一个项目……如果没有必要,我只是不想重新发明轮子。 编辑:我忘了一块。 我想用它作为测试工具。 因此,在上面的示例中,我可以快速创建一个包含随机但适当值的5个客户的列表。 然后说把它传递给我的持久性方法,并且有一些我可以检查的东西。 我试图避免每次为TDD目的手动创建一个填充的对象。 编辑2:好的,所以我开始自己开始…我将在本周末将它发布在Codeplex上并在此处链接…我显然不会这样做但是如果其他人想要继续工作它将会是一个开始它。