相关疑难解决方法(0)

找到一个有反射的私人领域?

鉴于这门课程

class Foo
{
    // Want to find _bar with reflection
    [SomeAttribute]
    private string _bar;

    public string BigBar
    {
        get { return this._bar; }
    }
}
Run Code Online (Sandbox Code Playgroud)

我想找到我将用属性标记的私有项_bar.那可能吗?

我已经用我寻找属性的属性做了这个,但从来没有私有成员字段.

获取私有字段需要设置哪些绑定标志?

.net c# reflection .net-attributes

219
推荐指数
8
解决办法
15万
查看次数

有没有办法使用反射在struct实例上设置属性?

我正在尝试编写一些在结构上设置属性的代码(重要的是它是结构上的属性)并且它失败了:

System.Drawing.Rectangle rectangle = new System.Drawing.Rectangle();
PropertyInfo propertyInfo = typeof(System.Drawing.Rectangle).GetProperty("Height");
propertyInfo.SetValue(rectangle, 5, null);
Run Code Online (Sandbox Code Playgroud)

高度值(由调试器报告)永远不会设置为任何值 - 它保持默认值0.

我之前已经对课程进行了大量的反思,但这种方法运行良好.另外,我知道在处理结构时,如果设置字段,则需要使用FieldInfo.SetValueDirect,但我不知道PropertyInfo的等效项.

c# reflection propertyinfo

48
推荐指数
2
解决办法
9837
查看次数

CryptoStream可以让基本流保持打开状态吗?

我创建了一个MemoryStream,传递给CryptoStream写作.我希望CryptoStream加密,然后让MemoryStream我开放,然后阅读其他内容.但是一旦CryptoStream被处置,它MemoryStream也会被处置掉.

可以以某种方式打开CryptoStream基地MemoryStream吗?

using (MemoryStream scratch = new MemoryStream())
{
    using (AesManaged aes = new AesManaged())
    {
        // <snip>
        // Set some aes parameters, including Key, IV, etc.
        // </snip>
        ICryptoTransform encryptor = aes.CreateEncryptor();
        using (CryptoStream myCryptoStream = new CryptoStream(scratch, encryptor, CryptoStreamMode.Write))
        {
            myCryptoStream.Write(someByteArray, 0, someByteArray.Length);
        }
    }
    // Here, I'm still within the MemoryStream block, so I expect
    // MemoryStream to still be usable. …
Run Code Online (Sandbox Code Playgroud)

c# stream objectdisposedexception

21
推荐指数
5
解决办法
4593
查看次数