小编Dan*_*cco的帖子

如何知道 propertyInfo 是否属于 C# 中的 IList 类型?

鉴于这个类:

public class SomeClass
{
    public int SomeProperty { get; set; }
    public IList<AnotherClass> MyList { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

而这段代码:

SomeClass myClass = new SomeClass();

PropertyInfo[] properties = myClass.GetType().GetProperties();
for(int i = 0; i < properties.Length; i++)
{
    //How can I figure that the current property is a collection/list?
}
Run Code Online (Sandbox Code Playgroud)

我尝试过的事情:

bool a1 = properties[i].PropertyType.IsAssignableFrom(typeof(IList));
//false
bool a2 = properties[i].PropertyType.IsAssignableFrom(typeof(IList<>));
//false
bool a3 = typeof(IList).IsAssignableFrom(properties[i].PropertyType);
//false
bool a4 = typeof(IList<>).IsAssignableFrom(properties[i].PropertyType);
//false
bool a5 = properties[i].PropertyType.Equals(typeof(IList));
//false
bool a6 = …
Run Code Online (Sandbox Code Playgroud)

c# reflection

2
推荐指数
1
解决办法
3254
查看次数

IL Emit调用泛型类型构造函数?

我正在尝试使用Emit在动态方法中创建一个新的List <>对象:

Type original; // original is a type passed

AssemblyName assemblyName = new AssemblyName("CustomAssembly");
AssemblyBuilder assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run);
ModuleBuilder _moduleBuilder = assembly.DefineDynamicModule("CustomModule");

// - IProxy can be ignored for this example
TypeBuilder typeBuilder = _moduleBuilder.DefineType(original.Name + "Proxy", TypeAttributes.Public | TypeAttributes.Class, original, new Type[] { typeof(IProxy) });

// - Getting the type of List<Interceptor>
Type interceptorList = typeof(List<>).MakeGenericType(typeof(Interceptor));
// - Setting a 'private List<Interceptor> _interceptors;'
FieldBuilder interceptorField = typeBuilder.DefineField("_interceptors", interceptorList, FieldAttributes.Private);
// - Getting the default constructor 'new List<Interceptor>()' …
Run Code Online (Sandbox Code Playgroud)

c# reflection.emit

2
推荐指数
1
解决办法
852
查看次数

C++映射变量地址?

我有这门课:

class Texture
{
    public:
        //I need this variable in this format
        float diffuseColor[3];
}
Run Code Online (Sandbox Code Playgroud)

但我想制作一个比处理"diffuseColor [0]"更简单的界面,例如:

myTexture.color.r = 1.0f; //this is diffuseColor[0]
Run Code Online (Sandbox Code Playgroud)

所以我试图得到一个类作为diffuseColor值的shell,类似于:

class Color
{
    public:
        float *r, *g, *b;
}
Run Code Online (Sandbox Code Playgroud)

在我的Texture类中:

class Texture
{
    public:
        Texture()
        {
            color.r = &diffuseColor[0];
            color.g = &diffuseColor[1];
            color.b = &diffuseColor[2];
        }

        Color color;
    private:
        float diffuseColor[3];
}
Run Code Online (Sandbox Code Playgroud)

但是现在的方式,如果我想使用它们,我必须取消引用颜色值:

(*myTexture.color.r) = 1.0f;
Run Code Online (Sandbox Code Playgroud)

如何在不想每次使用它的情况下取消引用它的情况下实现这一目标?

c++

1
推荐指数
1
解决办法
88
查看次数

如何在C++中获得TIME,最好是多平台

我一直在使用Windows的GetTickCount(),但我读过它的性能/分辨率很差,我想问一下最好的方法是什么.

我尝试使用<ctime>库,但它不会用于毫秒或微秒,我需要达到这个精度的东西.

谢谢!

c++

0
推荐指数
1
解决办法
455
查看次数

如果属性是任何类型的列表,则获取C#

我正在尝试根据我的类属性以下列模式创建一个文本编写器:

MyClass
    ID 1
    Name MyName
    AnotherProperty SomeValue
    ThisIsAnotherClass
        AnotherClassProperties 1

//Example class
public class MyClass
{
    public int ID { get; set; }
    public string Name { get; set; }
    public string AnotherProperty { get; set; }
    public AnotherClass ThisIsAnotherClass { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

所以我拿每个属性名称,写它,一个空格,然后是它的值(如果有的话).现在我正在尝试实现对列表和类似数组的支持,如下所示:

MyClass
    ArrayTest
        1
        2
        3
Run Code Online (Sandbox Code Playgroud)

如果它是一个类,我将对该函数进行递归,这样我就可以在这个模式中显示列表/数组中的所有值.(这是一个网络服务)

我的问题是,我怎样才能找到特定属性是否可以列表?

我试过了:

Type type = myObject.GetType();
PropertyInfo[] properties = type.GetProperties();
for(int i = 0; i < properties.Length; i++)
{
    if(properties[i].PropertyType.IsGeneric) //Possible List/Collection/Dictionary
    {
        //Here is my issue …
Run Code Online (Sandbox Code Playgroud)

c# reflection list

0
推荐指数
1
解决办法
96
查看次数

C#垃圾收集器的"KeepAlive"和线程

我有一个应用程序,对某人进行某种测试,持续约30分钟.在程序启动期间(第三方),我的代码被调用,我注册时间并将一些信息返回给应用程序,并将测试保存为"ONGOING".

大约30分钟后,我希望程序返回给我并将测试结果发给我,这样我就可以保存并完成整个过程.有时这种情况不会因各种原因而发生.所以我想在执行代码,以启动一个线程,睡了吧.大约35分钟(最长持续时间它可以),并且当它运行时只有一件事做:如果测试仍然是"正在进行",它更新它是"失败"或其他一些状态.

但是,我从读这里是C#的垃圾收集器能够收集它假定为不活动线程,我很担心这一点,所以我想请问如何做到这一点的KeepAlive方法适用于螺纹,或是否有任何可靠测试它的方法(因为即使我等了35分钟才发生它,可能会出现GC实际收集线程并且一切都失败的情况).

那么,这是什么工作的正确方法,或者如果不是这样,我该怎么做呢?

static void Main(string[] args)
{
    //After all the startup functions are done...
    Thread thread = new Thread( () => UpdateTest(1) );
    thread.Start();

    //My thread is supposedly safe...?
    GC.KeepAlive(thread);
}

static void UpdateTest(int testID)
{
    Thread.Sleep(210000); //Sleep for 35min

    //Search the database for the testID, update it if it's still ONGOING.
}
Run Code Online (Sandbox Code Playgroud)

.net c# multithreading garbage-collection

0
推荐指数
1
解决办法
315
查看次数