小编Oma*_*mar的帖子

有没有办法通过反思知道一个方法是来自调用者的汇编?

我们考虑以下代码:

internal class FooInternal
{
    internal static void DoIt()
    {
        throw new NotImplementedException();
    }
}
Run Code Online (Sandbox Code Playgroud)

现在我有一个库,里面反映了FooInternal类型,对于一个反映的methodInfo(在这种情况下是DoIt)我有这个:

if ((methodInfo.Attributes & MethodAttributes.FamANDAssem) == MethodAttributes.FamANDAssem)
{
     // hits here.
}
Run Code Online (Sandbox Code Playgroud)

我们可以看到我正在使用标记为FarmAndAssem的内部方法.现在,我还需要确保该方法在调用它的同一个程序集中被延迟,这样我的libary就不会在其他程序集中定义的类型中标记为internal的成员行.

我不能在反映方法的库中使用Assembly.GetExecutingAssembly,因为它将返回库所在的程序集,而不是使用库声明/调用类型的程序集.

因此,代码应确保以下工作流程:

 Given: FooInternal is declared in the same asssembly as the caller's assembly. 
 When: Called with MyLib.Fake(typeof(FooInternal)); 
 Then: It will hit the expected line.
 Given: A third-party type. 
 When: Called with MyLib.Fake(typeof(FileInfo)); 
 Then: It will not hit  the expected line even though *FileInfo* 
 has internal methods and as such …

.net c# reflection

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

在创建类时,如何填充IList?

我有以下课程:

public class CityViewModel
{
     public City City { get; set; }
     public IList<CityDetail> CityDetails { get; set; }

     public class CityDetail()
     {
         public CityDetail() {
         Text = new HtmlText();
         ImageFile = String.Empty;
         Correct = false;
         Explanation = new HtmlText();
     }
     public bool   Correct { get; set; }
     public HtmlText Text { get; set; }
     public string ImageFile { get; set; }
     public HtmlText Explanation { get; set; }
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能做到这样当我做以下事情时: var model = new CityViewModel();

它创造了CityViewModel十条CityDetail …

c# ilist

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

c#:如何删除IEnumerable中的Item

我正在制作一个自定义网格,接受IEnumerable作为Itemsource.但是我在删除方法期间无法删除itemsource中的Item.你们能帮我使用下面的代码吗?

static void Main(string[] args)
{
    List<MyData> source = new List<MyData>();
    int itemsCount = 20;
    for (int i = 0; i < itemsCount; i++)
    {
       source.Add(new MyData() { Data = "mydata" + i });
    }

    IEnumerable mItemsource = source;
    //Remove Sample of an mItemSource
    //goes here ..
}

public class MyData { public string Data { get; set; } }
Run Code Online (Sandbox Code Playgroud)

c#

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

c#:创建一个连接按钮

我有一个登录表单和一个按钮,用于检查用户名和密码是否为真.但问题是我试过的代码..我必须点击连接按钮两次.

但是当我单击按钮一次时,代码应该可以工作!对 ?

我认为问题是:如果只是点击某个按钮,它的DialogResult设置了某个值,showDialog就不会消失,所以在第一次点击时,connexionButton.DialogResult获取DialogResult.OK值,然后在第二次点击按钮执行代码.

*您可以注意到事件simpleButton1_Click是connexionButton Button的事件*

这是我用过的事件:

  this.connexionButton.Click += new System.EventHandler(this.simpleButton1_Click);
Run Code Online (Sandbox Code Playgroud)

这是我试过的代码:

private void simpleButton1_Click(object sender, EventArgs e)
{
     Boolean allowCnx = false;

     foreach (var row in myClass.ds.Tables["Users"].AsEnumerable())
     {
         if (row[1].ToString().ToLower() == idBox.Text.ToLower() && row[2].ToString().ToLower() == mdpBox.Text.ToLower())
         {
            allowCnx = true;
         }
     }

     if (allowCnx)
     {
         connexionButton.DialogResult = DialogResult.OK;
     }
     else
       XtraMessageBox.Show("Invalide Information", "Erreur", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
Run Code Online (Sandbox Code Playgroud)

这是我用来调用此登录表单的代码:

using (login loginForm = new login())
{
     var result = loginForm.ShowDialog();
     if (result == DialogResult.OK)
        this.Show();
     else
        this.Close();
}
Run Code Online (Sandbox Code Playgroud)

c# winforms

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

为什么我不能在没有方法的情况下设置类对象的属性值?

我在设置属性时遇到问题,并意识到我只能在函数或方法中设置属性.我的问题是为什么会这样?

这是我的代码有效:

public class SomeClass
{    
    Car car = new Car();

    public Car JustAMethod()
    {
        Car car = new Car();
        car.year = 2012;

        return car;
    }        
}
Run Code Online (Sandbox Code Playgroud)

为什么这不起作用:

public class SomeClass
{        
    Car car = new Car();
    car.year = 2012;//I get an error here
}
Run Code Online (Sandbox Code Playgroud)

c# function object

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

在C#中解析字符串时出现问题

我试图解析字符串的前三个字符.

public List<string> sortModes(List<string> allModesNonSorted)
{
     foreach (string s in allModesNonSorted)
     {
         char firstNumber  = s[0];
         char secondNumber = s[1];
         char thirdNumber  = s[2];

         char.IsDigit(firstNumber);
         char.IsDigit(secondNumber);
         char.IsDigit(thirdNumber);

         combinedNumbers = Convert.ToInt16(firstNumber) + Convert.ToInt16(secondNumber) + Convert.ToInt16(thirdNumber);
     }
     return allModesNonSorted;
}
Run Code Online (Sandbox Code Playgroud)

它正确识别每个字符,但增加了额外的值53或55.下面我添加数字时,包括53和55.为什么这样做?

c# char

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

字典到自定义KeyValuePair列表 - 无法转换(C#.Net 4.0)

我读到了字典和KeyValuePair不能由xml序列化程序编写.所以我编写了自己的KeyValuePair结构.

public struct CustomKeyValuePair<Tkey, tValue>
{
   public Tkey Key { get; set; }
   public tValue Value { get; set; }

   public CustomKeyValuePair(Tkey key,tValue value) : this()
   {
      this.Key = key;
      this.Value = value; 
   }
}  
Run Code Online (Sandbox Code Playgroud)

但是当我这样做时,我收到一个错误,它无法转换:

List<CustomKeyValuePair<string, AnimationPath>> convList = 
                   Templates.ToList<CustomKeyValuePair<string, AnimationPath>>();
Run Code Online (Sandbox Code Playgroud)

它适用于普通的keyValuePair,但不适用于我的自定义keyValuePair.所以有什么问题?我试图尽可能地复制原始文件,但它不想将我的字典(模板)转换为该列表.我看不到它使用任何接口或从结构继承来做到这一点.我是否必须手动添加所有条目?

.net c# dictionary

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

永久存储C#Winform中的用户选择

如何将用户的选择永久存储在c#winform中.我写了这段代码来获取设置:

string my_data_to_do = (string)Settings.Default["MyDataToDo"];
Run Code Online (Sandbox Code Playgroud)

为了保存用户的设置,我写道:

if (checkBox3.Checked)
{
   Settings.Default["MyDataToDo"] = "Tasks In Hand";
}
else
{
   Settings.Default["MyDataToDo"] = "Nothing To Do";
}
Run Code Online (Sandbox Code Playgroud)

这显示了已保存的数据,但直到我退出应用程序.当我再次退出并启动我的程序时,所有这些设置都会自动删除,并且默认数据会保存在Settings.settings文件中.

有人可以帮我吗?

.net c# winforms

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

创建可扩展的有限状态机

我想创建一个表示有限状态机的基类,然后由各种组件进行扩展.它应该尽可能通用,并且只允许执行专门为该机器设计的状态.这是我到目前为止所得到的:

public interface IState<out T> where T : FiniteStateMachine {
    void Enter( T p );
    void Execute( T p );
    void Exit( T p );
}

public class FiniteStateMachine {
    public IState<FiniteStateMachine> CurrentState { get; private set; }

    public void ChangeState( IState<FiniteStateMachine> s ) {
        if ( CurrentState != null )
            CurrentState.Exit( this );

        CurrentState = s;
        s.Enter( this );
    }
}

public class Car : FiniteStateMachine { }
public class Boat : FiniteStateMachine { }

public class CarState : …
Run Code Online (Sandbox Code Playgroud)

c# generics state-machine

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

结肠是什么意思?

我有一个类标题:

public class Person : Human
Run Code Online (Sandbox Code Playgroud)

什么:意思?

它类似于Java中的扩展吗?我需要它做什么?

c# windows class

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