使用反射,我需要加载同一组件的2个不同版本.我可以在同一个进程中在2个不同的AppDomain中加载2个版本吗?
我需要从旧版本的应用程序到新版本进行一些数据迁移.如果可能,请告诉我,或者我应该使用2个单独的流程.
C#,Net 2.0
这是代码(我拿出了所有特定于域的东西,它仍然返回一个空数组):
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
ChildClass cc = new ChildClass();
cc.OtherProperty = 1;
FieldInfo[] fi = cc.GetType().GetFields();
Console.WriteLine(fi.Length);
Console.ReadLine();
}
}
class BaseClass<T>
{
private int myVar;
public int MyProperty
{
get { return myVar; }
set { myVar = value; }
}
}
class ChildClass : BaseClass<ChildClass>
{
private int myVar;
public int OtherProperty
{
get { return myVar; }
set { …Run Code Online (Sandbox Code Playgroud) 假设我有一个名为Test的类,其中一个名为Title的属性带有自定义属性:
public class Test
{
[DatabaseField("title")]
public string Title { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
还有一个名为DbField的扩展方法.我想知道是否可以在c#中从对象实例获取自定义属性.
Test t = new Test();
string fieldName = t.Title.DbField();
//fieldName will equal "title", the same name passed into the attribute above
Run Code Online (Sandbox Code Playgroud)
可以这样做吗?
在我的应用程序中,我通过Web服务检索域对象.在Web服务数据中,我知道所有日期值都是UTC,但Web服务不会将其xs:dateTime值格式化为UTC日期.(换句话说,该字母Z不会附加到每个日期的末尾以表示UTC.)
我无法在此时更改Web服务的行为方式,但作为一种解决方法,我创建了一个方法,在Web服务中的对象被反序列化后立即调用该方法.
private void ExplicitlyMarkDateTimesAsUtc<T>(T obj) where T : class
{
Type t = obj.GetType();
// Loop through the properties.
PropertyInfo[] props = t.GetProperties();
for (int i = 0; i < props.Length; i++)
{
PropertyInfo p = props[i];
// If a property is DateTime or DateTime?, set DateTimeKind to DateTimeKind.Utc.
if (p.PropertyType == typeof(DateTime))
{
DateTime date = (DateTime)p.GetValue(obj, null);
date = DateTime.SpecifyKind(date, DateTimeKind.Utc);
p.SetValue(obj, date, null);
}
// Same check for nullable DateTime.
else if …Run Code Online (Sandbox Code Playgroud) 令人惊讶的是,以下代码未通过Assert:
int? wtf = 0;
Assert.IsType<Nullable<int>>(wtf);
Run Code Online (Sandbox Code Playgroud)
所以只是出于好奇,你怎么能确定给定的实例是否是Nullable <>对象?
在Java 6中,想象一下我有以下方法签名:
public void makeSandwich(Bread slice1, Bread slice2, List<Filling> fillings, boolean mustard)
Run Code Online (Sandbox Code Playgroud)
我想知道,在运行时,传递给slice2或任何其他参数的值,这里重要的一点是我想通过参数名称获取值.
我知道如何用getParameterTypes或获取参数类型列表getGenericParameterTypes.
理想情况下,我想获得一个参数名称列表而不是类型.有办法吗?
让我们假设一个人检索Field使用反射的声明类型.
以下哪项测试将正确表明是否正在处理一个int或一个Integer?
Field f = ...
Class<?> c = f.getDeclaringClass();
boolean isInteger;
isInteger = c.equals(Integer.class);
isInteger = c.equals(Integer.TYPE);
isInteger = c.equals(int.class);
isInteger = ( c == Integer.class);
isInteger = ( c == Integer.TYPE);
isInteger = ( c == int.class);
Run Code Online (Sandbox Code Playgroud) Type.GetType("TheClass");
Run Code Online (Sandbox Code Playgroud)
null如果namespace不存在则返回如下:
Type.GetType("SomeNamespace.TheClass"); // returns a Type object
Run Code Online (Sandbox Code Playgroud)
有没有办法避免给出这个namespace名字?
Python的函数装饰器是否有C#模拟?感觉它可以使用属性和反射框架,但我没有看到在运行时替换函数的方法.
Python装饰器通常以这种方式工作:
class decorator(obj):
def __init__(self, f):
self.f = f
def __call__(self, *args, **kwargs):
print "Before"
self.f()
print "After"
@decorator
def func1():
print "Function 1"
@decorator
def func2():
print "Function 2"
Run Code Online (Sandbox Code Playgroud)
然后调用func1和func2将导致
Before Function 1 After Before Function 2 After
我们的想法是装饰器可以让我轻松地在多个功能的入口和出口点添加常见任务.
我正在尝试使用反射来获取在类中显式声明的所有公共方法(因此它c.getMethods()也不起作用,因为它也抓取了超类方法).我可以用
Method[] allMethods = c.getDeclaredMethods();
Run Code Online (Sandbox Code Playgroud)
从这个类中获取方法,但我只想使用公共方法.
此时,我正在尝试获取修改器并基于此执行某些操作,但由于某种原因,调试器中显示的修饰符值和修饰符值输出不同.例如,我有一个私有getNode方法,虽然"修饰符"值出现2在调试器中,但它会像"1"我一样输出System.out.println(c.getModifiers()).奇怪的.有另一种方法可以获得公共方法,还是我错过了一些明显的东西?谢谢你的帮助!