system.type

Difference between IsGenericType and IsGenericTypeDefinition

我们两清 提交于 2019-12-22 03:21:46
问题 What is the difference between Type.IsGenericType and Type.IsGenericTypeDefinition ? Interestingly enough, MSDN's link for IsGenericTypeDefinition is broken . After playing a bit with trying to retrieve all the DbSets defined in a given DbContext, I was lead to the following, which behavior I am trying to understand: filtering properties via IsGenericType returns the desired results, while with IsGenericTypeDefinition not (does not return any). It's interesting that from this post I have the

C# type name instead of CLR type name

限于喜欢 提交于 2019-12-19 09:28:09
问题 typeof(int).Name Will return System.Int32 does anyone know of a way to return "int" 回答1: There aren't many types that are C# keywords (int, double, string) ... so perhaps you can write your own mapping function, from the System type name to the corresonding C# keyword. 回答2: Yeah, you could write a mapping function. That is just an alias anyway. Here is a list: http://msdn.microsoft.com/en-us/library/86792hfa(VS.71).aspx 回答3: Using reflection, you can use CSharpCodeProvider (exists in .NET

Looping over derived DbContext DbSets so I can add a ClearAll() extension to my DbContext object

a 夏天 提交于 2019-12-17 21:17:56
问题 Based on [the difference between Type.IsGenericType and Type.IsGenericTypeDefinition ][1], I gained the piece of information I was missing in order to be able to retrieve a DbContext's DbSets via Reflection. However, I am now stuck somewhere else. In the code sample below, I do succeed in obtaining the DbSet generic instances, but I now want to be able to cast them at compile time, to the specific generic type defintion. dbSet is a real instance of one of the DbSets<...> from my DbContext,

IoC spring.net injecting System.Type

断了今生、忘了曾经 提交于 2019-12-11 07:36:47
问题 I am trying to initiate a class that is expecting a System.Type in it's CTOR. Is there a way in spring.net config file to accomplish this, and preferable pass the type of a spring initialised object? Thanks, Lihnid 回答1: I think that this should do it: <constructor-arg name="argumentname" expression="T(namespace.to.my.type, assemblyname)" /> 回答2: This also works: <constructor-arg name="argumentname" value="MyNamespace.MyType, MyAssembly"/> Use VS2010 Add-In for intellisense: http://www

Best way to check if System.Type is a descendant of a given class

为君一笑 提交于 2019-12-06 17:19:07
问题 Consider the following code: public class A { } public class B : A { } public class C : B { } class D { public static bool IsDescendantOf(this System.Type thisType, System.Type thatType) { /// ??? } void Main() { A cValue = new C(); C.GetType().IsDescendantOf(cValue.GetType()); } } What is the best way to implement IsDescendantOf? 回答1: Type.IsSubclassOf() Determines whether the class represented by the current Type derives from the class represented by the specified Type. 回答2: You are

C# type name instead of CLR type name

∥☆過路亽.° 提交于 2019-12-01 08:43:30
typeof(int).Name Will return System.Int32 does anyone know of a way to return "int" There aren't many types that are C# keywords (int, double, string) ... so perhaps you can write your own mapping function, from the System type name to the corresonding C# keyword. BobbyShaftoe Yeah, you could write a mapping function. That is just an alias anyway. Here is a list: http://msdn.microsoft.com/en-us/library/86792hfa(VS.71).aspx Using reflection, you can use CSharpCodeProvider (exists in .NET Core 2 as well) to get "int", "string", etc. instead of the full CLR Type names. There are other SO posts

Protobuf-Net error message: No Serializer defined for type: System.Type

无人久伴 提交于 2019-11-29 08:39:13
问题 I am getting the following error message when trying to serialize List<Tuple<string, Type, object>> : No Serializer defined for type: System.Type I tried both, just serializing the above collection or serializing a class that has the same collection defined as protoMember. Both result in the same error message. Is this a non-supported type? I assume it is supported and I overlooked something else but maybe I am incorrect? Thanks for any pointers that may help resolve this... 回答1: Edit:

Looping over derived DbContext DbSets so I can add a ClearAll() extension to my DbContext object

喜欢而已 提交于 2019-11-28 14:31:10
Based on [the difference between Type.IsGenericType and Type.IsGenericTypeDefinition ][1], I gained the piece of information I was missing in order to be able to retrieve a DbContext's DbSets via Reflection. However, I am now stuck somewhere else. In the code sample below, I do succeed in obtaining the DbSet generic instances, but I now want to be able to cast them at compile time, to the specific generic type defintion. dbSet is a real instance of one of the DbSets<...> from my DbContext, but how to cast it to its real type definition ? (Take the 1st loop iteration, dBSet will represent DbSet

How return the type of a System.__COMObject in System.Type in C#

牧云@^-^@ 提交于 2019-11-28 00:42:29
问题 I'm doing a program and I want to do a Reflection, but for this, I need an Object of the Type class, right? to use the .GetProperties() method... So I tryed this: Type typeName = simObjects.getType(); But the .GetType() is returning "System.__COMObject". And this is not helpfull. The same happens with .typeof(). I search and found another code, this one: Type typeName = (Type)Microsoft.VisualBasic.Information.TypeName(simObjects); But this method return a String, and I need it in System.Type,

Is there a better alternative than this to &#39;switch on type&#39;?

末鹿安然 提交于 2019-11-25 23:04:05
问题 Seeing as C# can\'t switch on a Type (which I gather wasn\'t added as a special case because is relationships mean that more than one distinct case might apply), is there a better way to simulate switching on type other than this? void Foo(object o) { if (o is A) { ((A)o).Hop(); } else if (o is B) { ((B)o).Skip(); } else { throw new ArgumentException(\"Unexpected type: \" + o.GetType()); } } 回答1: Switching on types is definitely lacking in C# ( UPDATE: in C#7 / VS 2017 switching on types is