【c#-EF核心实体类型】教程文章相关的互联网学习教程文章

如何在通用函数中使用的C#泛型类中查找基础类型?【代码】

假设我有一个C#类型:class MyType<T> {T Value { get; set; }// etc. }还有一些特殊的类型:class MyTypeString : MyType<string> {// etc. }class MyTypeDate : MyType<Date> {// etc. }我想编写一个泛型函数,我将能够操作(甚至创建)MyTypeString或MyTypeDate类型的对象,同时返回/修改基础类型值(字符串或日期). 我目前的解决方案是这样的:T_Underlying Foo<T, T_Underlying>(ref T p_variable, T_Underlying p_value)where T :...

知道对象c#中的属性类型【代码】

我知道如何使用反射获取对象属性:var properties = typeof(T).GetProperties();现在我怎么知道properties [0]是否是一个字符串?或者它可能是一个int?我怎么知道?解决方法:属性的每个元素都是PropertyInfo,它具有PropertyType属性,指示属性的类型. 例如,您可以使用:if (properties[0].PropertyType == typeof(string))或者如果您想以继承许可的方式检查某些内容:if (typeof(Stream).IsAssignableFrom(properties[0].PropertyT...

c# – 如何将默认参数值设置为BigInteger类型?【代码】

我需要一个具有BigInteger类型的构造函数的类.我想用这个代码将默认值设置为0,但是我遇到了编译错误.public class Hello {BigInteger X {get; set;}public Hello(BigInteger x = 0){X = x;} }public class MyClass {public static void RunSnippet(){var h = new Hello(); // Error <-- 怎么了?有没有办法将默认值设置为BigInteger参数?解决方法:默认参数仅适用于编译时常量(以及参数可以是默认值(ValType)或新ValType())的值类型...

C#托管代码引用类型问题【代码】

我有一个包含一些属性的类,包括列表.这些列表可以填充一些可能相当占用内存的Sharepoint对象. 我将这个类及其列表属性传递给我的函数,如下所示:public void InsertFixedLineItems(CacheBundle cb){ //work here}正如您所看到的那样,有问题的类型称为CacheBundle,并且在运行时它的人口密集. 为了便于使用,我想进一步本地化精确的列表属性,如下所示:public void InsertFixedLineItems(CacheBundle cb){List<XYZCacheItem> XYZCache...

c#基础之数据类型【代码】

(一)字符串 (1)字符串类型是不可变的,是引用类性。如果有大量字符串需要修改,可以用StringBuilder,它包含了Append(),AppendFormat(),Insert(),Remove(),Replace()等方法。两者的区别在于 这些方法会修改StringBuilder本身中的数据,而不是简单的返回一个新字符串。 (2)null表示将变量设置成无,且只能赋给引用类性,表示这个变量无任何值,不同于"",它表示为空字符串。 (3)隐式类性的局部变量var。但是在...

c# – 正确地将匿名类型声明为数组以保持范围【代码】

我想要做的就是正确地声明var place,这样一旦我到达foreach循环它仍然在范围内.我假设我需要在连接的if语句之前声明它.这是一个正确的假设,如果是这样,我该如何申报?谢谢!using (var db = new DataClasses1DataContext()){if (connections == "Connections"){var place = (from v in db.pdx_apartswhere v.Latitude != null && v.Region == region && v.WD_Connect >= 1select new{locName = v.Apartment_complex.Trim().Replace...

c# – 仅在一个类中设置泛型类型【代码】

我有两个通用类:public class First<T> {... } public class Second<T> {... }我使用Second类作为First类构造函数的参数:var instance = new First<int>(new Second<int>());是否可以仅为First类构造函数指定泛型类型(在我的示例中为整数),如下所示:var instance = new First<int>(new Second());?解决方法:我相信通用构造函数参数的类型推断正被添加到C#6中.现在,答案是否定的.

c# – 泛型类型的索引器约束【代码】

是否可以创建一个类型必须具有索引器的泛型类/方法? 我的想法是让以下两种扩展方法适用于使用索引器获取和设置值的任何类型,但似乎无法找到任何关于它的内容.只有关于使索引器本身通用的东西,这不是我追求的……public static T GetOrNew<T>(this HttpSessionStateBase session, string key) where T : new(){var value = (T) session[key];return ReferenceEquals(value, null) ? session.Set(key, new T()) : value;}public sta...

C#基于类型将类实现接口转换为Class【代码】

我的问题最好用一个例子来解释:public IPlotModel MyPlotModel; public ??? CastedModel;public Constructor() {MyPlotModel = new PlotModel(); //This should be interchangable with other types//E.g.: MyPlotModel = new OtherModel();CastedModel = (MyPlotModel.GetType()) MyPlotModel; }这基本上就是我想要的.CastedModel应该转换为MyPlotModel的类型. 我使用Convert.ChangeType()取得了一些成功.我设法将CastedModel的类...

c# – ‘Models.LoginViewModel’是一种类型,在给定的上下文中无效【代码】

我的代码是:@sample.Models.LoginViewModel @{string user = Session["users"].ToString(); } @{ViewBag.Title = "Home Page"; }我收到错误:‘@sample.Models.LoginViewModel’ is a type, which is not valid in the given context. 请提出任何解决我的问题的想法.解决方法:改变代码@sample.Models.LoginViewModel如@model sample.Models.LoginViewModel即是@model sample.Models.LoginViewModel@{if(Session["users"] != nul...

C#EmptyIfNull扩展,用于任何IEnumerable返回空派生类型【代码】

假设nulls和空集合是等价的,我正在尝试为IEnumerable类型编写一个扩展方法,以返回派生类型的空集合而不是null.这样我就不必在整个地方重复进行空检查,而且我没有得到一个我必须强制转换的IEnumerable. 例如List<Foo> MethodReturningFooList() { ... }Foo[] MethodReturningFooArray() { ... }void Bar() {List<Foo> list = MethodReturningFooList().EmptyIfNull();Foo[] arr = MethodReturningFooArray().EmptyIfNull(); }public...

c# – 错误:此类型的CollectionView不支持对其SourceCollection的更改【代码】

我有一个ObservableCollection项,我需要能够更新并使用ICollectionView表示数据. 以下是相关的代码:private ObservableCollection<Hero> heroesDBHeroes; public ObservableCollection<Hero> HeroesDBHeroes {get{return heroesDBHeroes;}set{heroesDBHeroes = value;OnPropertyChanged("HeroesDBHeroes");} } private void HeroesDBAddHeroes() {if(HeroesDBHeroes != null){HeroesDBHeroes.Clear();}HeroesDBHeroes = Hero.GetA...

在c#中将2个价格作为数据类型字符串进行比较【代码】

我想知道如何将2个价格作为数据类型字符串进行比较. 例;string oldPrice = "£1.99"; string newPrice = "£2.50";我想比较newPrice是> =到oldPrice,但我不确定如何将字符串转换为decimal / int.脱掉£符号. 有任何想法吗?关于如何解决这个问题的提示或技巧?解决方法:这应该工作:string oldPrice = "£1.99";decimal result = decimal.Parse(oldPrice, System.Globalization.NumberStyles.Currency);将货币存储为双倍并不是一个好...

func上的C#可选参数丢失泛型类型推断【代码】

我有琐碎的通用枚举解析方法:public static T Parse<T>(string value) => (T)Parse(typeof(T), value, false);用法是:public IEnumerable<DocumentTypesEnum> FileTypeEnums => FileTypes.Split(',').Select(Extensions.StringEnumerator.Parse<DocumentTypesEnum>);以上编译很好. 将可选参数添加到Parse< T>:public static T Parse<T>(string value, bool ignoreCase = false) => (T)Parse(typeof(T), value, ignoreCase);现在...

c# – 操作符’||’不能应用于’bool’类型的操作数?和’布尔?’【代码】

我想检查两个字符串中的一个是否包含部分字符串.知道firstString或secondString可以为null,我尝试了以下内容:string firstString= getValue();string secondString= getValue();if (firstString?.Contains("1") || secondString?.Contains("1")){//logic}我收到编译时错误,提到:Operator ‘||’ cannot be applied to operands of type ‘bool?’ and‘bool?’看起来像String的结果?.Contains(“1”)是可以为空的布尔值. 这个错...