小编Adi*_*ter的帖子

在2点分割一个字符串

我有一个名为file_test1.txt的文件,我想从名称中提取test1并将其放在一个字符串中.这是最好的方法吗?

例如

string fullfile = @"C:\file_test1.txt";
string section = [test1] from fullfile;      // <- expected result
Run Code Online (Sandbox Code Playgroud)

我希望能够分割'file_''.txt',因为'test1'部分可能更大或更小,但'file_''.txt'将始终相同.

c# c#-4.0

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

条件属性如何在幕后工作

我们有这个代码:

public static class MyCLass 
{
    [Conditional("Debugging")]  
    public static void MyMethod()
    {
         Console.WriteLine("Example method");
    }
}
.
.
.
//In debug mode: Executing Main method in debug mode
MyClass.MyMethod()
Run Code Online (Sandbox Code Playgroud)

我想知道的是条件属性如何改变MyMethod的行为,假设在.NET中Conditional属性定义为:

public class Conditional: Attribute
{
   .
   .
   public string Mode { get; set; )
   .
   .
   public Conditional(string Mode)
   {
       .
       .
       this.Mode = Mode;
       if (Mode == "Debugging")
       {
          #ifdef DEBUG
          //HOW THE CONDITIONAL CONSTRUCTOR COULD CHANGE THE BEHAVIOUR OF MyMethod
          #endif
       }
       .
       .
   }
}
Run Code Online (Sandbox Code Playgroud)

如何访问由我的属性(即来自MyAttribute类)修饰的资源(方法,成员,类......)?

.net c# debugging conditional custom-attributes

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

将值传递到ConstructorInfo.Invoke

我正在使用反射来构造一个类对象,该类对象接受一个intas构造函数参数。我ConstructorInfo.Invoke用来创建课程。谁能告诉我如何将参数传递给构造函数?我正在尝试以下方法没有运气

Assembly ass = Assembly.GetExecutingAssembly();
            Type typa = ass.GetType("Abc");
            Type[] types = new Type[1];
            types[0] = typeof(int);
            ConstructorInfo csInfo =typa.GetConstructor(types);
            int[] obj = {10};

            csInfo.Invoke(obj);
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

参数1:无法从“ int []”转换为“ object []”

c#

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

Lambda表达式 - 它是否正确评估?

我有以下代码似乎没有正确行事.有一个属性有一个属性不是类型FieldMapAttribute,但它仍然进入if条件,我检查匹配该类型属性的计数.

foreach (PropertyInfo _property in _properties)
{
    var attributes = _property.GetCustomAttributes(false);
    if (attributes.Select(a => a.GetType() == typeof(FieldMapAttribute)).Count() > 0)
    {
        colname = (attributes.Select(a => a.GetType() == typeof(FieldMapAttribute)).Cast<FieldMapAttribute>().First()).DbColumnName;
    }
}
Run Code Online (Sandbox Code Playgroud)

有人可以帮我理解这里发生了什么吗?

.net c# linq custom-attributes

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

永久锁定文件

我正在 VS2010 上使用 C# 开发文件锁定器/解锁器应用程序。我想要的是使用我的应用程序使用密码锁定文件,然后随时解锁。

事实上,我使用以下代码来锁定文件,但仅当应用程序仍在运行时才会锁定文件;当我关闭应用程序时,文件被解锁。

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Configuration;
using System.Windows.Forms;

namespace LockFile
{
    public enum LockStatus
    {
        Unlocked,
        Locked
    }

    public class LockFilePresenter
    {
        private ILockFileView view;
        private string file2Lock = string.Empty;
        private FileStream fileLockStream = null;

        public LockFilePresenter(ILockFileView view)
        {
            this.view = view;
        }

        internal void LockFile()
        {
            if (string.IsNullOrEmpty(file2Lock) || !File.Exists(file2Lock))
            {
                view.ShowMessage("Please select a path to lock.");
                return;
            }

            if (fileLockStream != null)
            {
                view.ShowMessage("The path is already locked."); …
Run Code Online (Sandbox Code Playgroud)

c# encryption file-locking filestream

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