小编Dmi*_*nko的帖子

在c#中通过反射向对象添加属性

我想创建一个接收 3 个字符串作为参数并返回一个对象的方法,该对象包含它们引用这些字符串的三个属性。

没有要复制的“旧对象”。应在此方法中创建属性。

是在C#中用反射来做到这一点吗?如果是这样,怎么办?下面是你喜欢而我做不到的。

protected Object getNewObject(String name, String phone, String email)
{
    Object newObject = new Object();

    ... //I can not add the variables that received by the object parameter here.

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

c# reflection properties

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

c#中的未知运算符

嗨,我不是C#的专家,我发现这段代码,并不真正理解它的作用.

我之前从未见过=>c#中的运算符.就像一个重定向?

public byte[] methodA(byte[] data) => 
  this.methodB(data);
Run Code Online (Sandbox Code Playgroud)

c#

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

如何在C中读取一个整数后跟一个字符串?

我正在尝试编写一个简单的程序,它将读取两个输入行,一个整数后跟一个字符串。然而,它似乎对我不起作用。

int main()
{
    int i;
    char str[1024];

    scanf("%d", &i);
    scanf("%[^\n]", str);
    printf("%d\n", i);
    printf("%s\n", str);

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

输入整数并按“Enter”后,程序立即打印该整数。它不会等我输入字符串。怎么了?对此进行编程的正确方法是什么?

c

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

如何使用 LINQ 将数组转换为字典

我有一个字符串数组,我想Dictionary使用Linq将其转换为 a 。我想和一个元件偶数索引(包括零)是键与和元件奇数索引是值在字典中。我使用以下方法创建了一个字典for loop:

string[] arr = new string[4];
arr[0] = "John";
arr[1] = "A";
arr[2] = "Luke";
arr[3] = "B";

Dictionary<string, string> myDict = new Dictionary<string, string>();

for (int i = 0; i < arr.Length - 1; i += 2)
{
    myDict.Add(arr[i], arr[i + 1]);
}

//myDict -> { { "John", "A" },{"Luke","B"} }
Run Code Online (Sandbox Code Playgroud)

现在我很好奇如何用LINQ 做到这一点ToDictionary():

myDict = arr.ToDictionary();
Run Code Online (Sandbox Code Playgroud)

c# linq arrays dictionary

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

数组中两个不同元素之间的最大距离

我有一个问题,我需要找到一个数组中两个不同元素之间的最大距离.

例如:给定一个数组4,6,2,2,6,6,4,该方法应返回5最大距离.

我能够使用两个for循环来解决问题,但它不是一个优化的解决方案.我试图通过在单个for循环中进行优化来优化它.

这是我目前的解决方案:

int [] A = {4,6,2,2,6,6,4};
int N = A.length;
int result = 0;

for (int i = 0; i < N; i++){
    for (int j = i; j < N; j++) {
        if(A[i] != A[j]){
            result = Math.max(result, j - i);
        }
    }
}

// tried below code but it is not efficient
//      for (int i = 0; i < N; i++){
//          
//          if(A[N-1] != A[i]){
//              result = Math.max(result, …
Run Code Online (Sandbox Code Playgroud)

java arrays algorithm integer

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

将动态对象转换为 .NET C# 中的具体对象

正如你从标题中看到的,我需要转换这个对象:

    object obj = new{
       Id = 1,
       Name = "Patrick"
    };
Run Code Online (Sandbox Code Playgroud)

到特定的类实例。

为了更清楚,这里有一个例子给你们:

    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }

    }

    public class Scholar
    {
        public int UniqueId { get; set; }
        public string FullName { get; set; }

    }
Run Code Online (Sandbox Code Playgroud)

我有两个班级Student和Scholar。我无法找到一种正确编写转换为特定类型的算法的方法。

在我看来,伪代码应该是这样的:

if (obj.CanBeConverted<Student>()) {
   //should return this if statement

   obj = ConvertToType<Student>(o);

   // after this method obj type should change to Student
} …
Run Code Online (Sandbox Code Playgroud)

c# reflection dynamic object

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

如何将文本字符串数组的数量限制为15

如何将从文档中拉出的线条一次限制为15行.现在它一次显示所有行.谢谢.

class Program {
  static void Main(string[] args) {
    string[] lines = System.IO.File.ReadAllLines(
        @"C:\Users\chri749y\Documents\Skrive til fil\Testprogram.txt");

    foreach (string line in lines) {
        Console.WriteLine("{0}", line);
    }

    Console.ReadKey();
  }
}
Run Code Online (Sandbox Code Playgroud)

c#

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

附加没有硬编码值的字符串

我有一个字符串,看起来像:

A5050MM
Run Code Online (Sandbox Code Playgroud)

我想要实现的是追加字符串的结尾,使其变为:

A5050MM01
A5050MM02
...
Run Code Online (Sandbox Code Playgroud)

目前我这样做如下:

string sn = "A5050MM";

for (int i = 0; i <= 99; i++)
{
   string appendVal = i < 10 ? "0" + i : i.ToString();
   string finalsn = string.Concat(sn, appendVal);
   Console.WriteLine(finalsn);
}
Run Code Online (Sandbox Code Playgroud)

这可以工作,但你可以看到我正在硬编码,"0"因为如果我没有,那么输出将A5050MM1, A5050MM2 ...一直到9.

我的问题是有另一种方式来实现这一目标没有硬编码的"0"或者这是唯一的方法,我将不得不跟进?

在此先感谢您的帮助.

.net c# string

4
推荐指数
2
解决办法
103
查看次数

在同一个类c#中的构造函数中调用构造函数,并在同一个构造函数中调用base Constructor

我有class A继承自class B. class B有以下构造函数:

public class B
{
    public B(int num) { ... }
}
Run Code Online (Sandbox Code Playgroud)

Class A有一个默认的构造函数.有没有实现构造函数的方式class A它调用基类的构造从class B并调用默认的构造函数的class A?可以使用的东西base和this:

public class A : B
{
    public A() { ... }

    public A(int num) : base(num), this()
    { ... }
}
Run Code Online (Sandbox Code Playgroud)

c# constructor

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

提供4位数字长的序列号失败

我必须获取序列号,其中包括Member从用户那里获得的序列号,并将其添加到4位成员的序列号中。如果我得到888了newMemberIdwill将8880001,然后在下一个循环中8880002,依此类推,问题是当获得十个索引时,我将获得888010此代码。为什么?

while (dbNof.AllMembers.Any(x => x.MemberId == newMemberId))
{
    numerator++;

    newMemberId = Member + 
                  numerator.ToString().PadLeft(5-numerator.ToString().Length, '0');
}
Run Code Online (Sandbox Code Playgroud)

c# string

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

标签 统计

c# ×8

arrays ×2

reflection ×2

string ×2

.net ×1

algorithm ×1

c ×1

constructor ×1

dictionary ×1

dynamic ×1

integer ×1

java ×1

linq ×1

object ×1

properties ×1