- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我有以下类结构:
public class Outer<T>
{
public class Inner<U>
{
}
}
还有一些代码:
var testType = typeof(Outer<string>.Inner<int>);
如何获取构造的泛型类型 typeof(Outer<string>)
,或通用值 typeof(string)
来自testType
变量?
最佳答案
有趣 - 外部类型的泛型参数似乎被投影到内部类型:
var testType = typeof(Outer<string>.Inner<int>);
var outerType = testType.DeclaringType;
var outerTypeGenericParam = outerType.GetGenericArguments();
var testTypeGenericParam = testType.GetGenericArguments();
Console.WriteLine(outerType); // Test+Outer`1[T]
Console.WriteLine(outerTypeGenericParam[0]); // T
Console.WriteLine(testTypeGenericParam[0]); // System.String
Console.WriteLine(testTypeGenericParam[1]); // System.Int32
所以你的情况中的一句话是:
testType.GetGenericArguments()[0]
关于c# - 使用反射从 C# 中嵌套类型的 DeclaringType 获取泛型类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55051292/
我正在做一个 Func -> Expression -> Func 转换。如果我从一个方法(下面的第一个示例)创建 Func<>(),它工作正常,但是如果我使用表达式树(第二个示例)创建函数,它会在访
我正在 GitHub 上浏览 System.Linq.Expressions 源代码并找到了 following code public static NewExpression New(Constr
我有以下类(class): public class Foo { public virtual string FooProperty { get; set; } } public class
我正在使用 ReSharper(旧版本),它曾经在我使用 PropertyInfo.DeclaringType 时警告我它可以是 null。 第一眼和第二眼对我来说没有意义。这是真的,还是 ReSha
谁能说出这两个属性之间的区别? DeclaringType 和 ReflectedType 考虑代码是: public class TestClass { public static void
假设我有以下类结构: public class Outer { public class Inner { } } 还有一些代码: var testType = typeof(O
Resharper 告诉我 MemberInfo.DeclaringType 永远不能为空: 但是,当该代码运行时,会打印文本“Top level member”。我不明白,这里有什么问题? 最佳答案
当我想使用 log4net 可用的不同日志记录级别时,我发现我需要使用 ILogger.Log 方法,它看起来像这样: void Log(Type callerStackBoundaryDeclari
代码: static Func CompileGetValueExpression(PropertyInfo propertyInfo) { var instance = Expression
我正在为try/catch语句创建一个简单的日志记录方法(没什么花哨的)。我要包括类名称和引发异常的方法。 有两种获取信息的方法。一种使用MethodBase.GetCurrentMethod()和异
我是一名优秀的程序员,十分优秀!