草庐IT

c# - 如何从基类中获取子类的 `Type`

我有一个抽象基类,我想在其中实现一个方法来检索继承类的特性属性。像这样的……publicabstractclassMongoEntityBase:IMongoEntity{publicvirtualobjectGetAttributeValue(stringpropertyName)whereT:Attribute{varattribute=(T)typeof(this).GetCustomAttribute(typeof(T));returnattribute!=null?attribute.GetType().GetProperty(propertyName).GetValue(a

c# - XML 序列化错误 : 2 types both use the XML type name, 'Relationship',来自命名空间 ''

我在通过XML进行序列化时遇到问题,因为2个类使用一个类(尽管类不同!)称为关系。我尝试使用XML属性用另一个名称装饰其中的一个类,但它仍然给我以下错误:{"Types'SiteServer.Relationship'and'LocalServer.Relationship'bothusetheXMLtypename,'Relationship',fromnamespace''.UseXMLattributestospecifyauniqueXMLnameand/ornamespaceforthetype."}这是我的2个类(class),有人知道为什么吗??我使用了错误的属性吗?它似

c# - 保留一个 Dictionary<Type, MyClass<T>> ,其中元素可以按类型引用

我有一个名为EntityTypeTransform的抽象类,它有一个抽象方法,用于保存将IDataRecord转换为T实例的Func委托(delegate)。publicabstractclassEntityTypeTransformwhereTEntityType:class{publicabstractFuncGetDataTransform();}该类的实现可能看起来像(看起来像):publicclassTaskParameterEntityTypeTransform:EntityTypeTransform{publicoverrideFuncGetDataTransform()

c# - 编译转换 : The type 'Object' is defined in an assembly that is not referenced

我正在对一个asp.NetMVC5网络应用程序进行一些更改,我在其中使用了typelite从C#类创建.ts定义(非常方便)。出于某种原因,现在我在执行T4时遇到了这个错误:Compilingtransformation:Thetype'Object'isdefinedinanassemblythatisnotreferenced.Youmustaddareferencetoassembly'mscorlib,Version=2.0.5.0,Culture=neutral,PublicKeyToken=7cec85d7bea7798e,Retargetable=Yes'.和这个警告:C

c# - 使用 Type 变量转换对象

当然,以下是行不通的。有没有一种可能的方法,与此非常相似?TypenewObjectType=typeof(MyClass);varnewObject=givenObjectasnewObjectType; 最佳答案 newObjectType是Type类(包含有关类型的元数据)的实例,而不是type本身.这应该可行varnewObject=givenObjectasMyClass;或varnewObject=(MyClass)givenObject;转换为类型实例确实没有意义,因为编译时必须知道变量类型应该是什么,而类型实例是运行

c# - 包含路径表达式必须引用 type.in 预加载中定义的导航属性

我尝试像这样包含匿名类型:除了CompanyTitle,PeriodTypeName之外,我还想要所有incomelist属性varincomeList=ctx.IncomeLists.Include(i=>new{CompanyTitle=i.CompanyId.ToString()+"/"+i.Company.CompanyName,PeriodTypeName=i.ListPeriods.Select(lp=>lp.PeriodType.PeriodTypeName)}).ToList()我的模型部分是这样的:但我得到以下异常:TheIncludepathexpressionmu

C# 功能请求 : implement interfaces on anonymous types

我想知道做这样的事情需要什么:usingSystem;classProgram{staticvoidMain(){varf=newIFoo{Foo="foo",Print=()=>Console.WriteLine(Foo)};}}interfaceIFoo{StringFoo{get;set;}voidPrint();}创建的匿名类型看起来像这样:internalsealedclassf__AnonymousType0j__TPar>:IFoo{readonlyj__TPari__Field;publicf__AnonymousType0(j__TParFoo){this.i__Fi

c# - 在 C# 中使用 Type.GetType() 返回的类型

我有一个关于如何可能的问题(如果可能的话:)例如,使用Type.GetType()返回的类型引用来创建该类型的IList?这里是示例代码:Typecustomer=Type.GetType("myapp.Customer");IListcustomerList=newList();//gotanerrorhere=[提前致谢! 最佳答案 像这样:TypelistType=typeof(List).MakeGenericType(customer);IListcustomerList=(IList)Activator.CreateIn

c# - 使用 MultipartFormDataContent 生成错误的 Content-Type header

我有以下代码:privatestaticstringboundary="----CustomBoundary"+DateTime.Now.Ticks.ToString("x");privatestaticasyncTaskPostTest(){stringservResp="";using(varcontent=newMultipartFormDataContent(boundary)){content.Add(newStringContent("105212"),"case-id");content.Add(newStringContent("1/14/2014"),"dateFro

c# - 序列化异常 : Type is not resolved for member "..."

我一直在尝试将程序集动态加载到AppDomain。我需要这样做是因为我想动态调用一个方法,但在我的应用程序运行时不要保留DLL的句柄,以便在需要时可以替换它。但我收到此SerializationException异常:类型未解析成员“...”这是我的代码:AppDomaindomain=AppDomain.CreateDomain("TempAppDomain",null,AppDomain.CurrentDomain.SetupInformation);try{objectobj=domain.CreateInstanceFromAndUnwrap(dllPath,typeName)