相关疑难解决方法(0)

在c#.net中的现有数组中添加新项

如何在C#.net中的现有字符串数组中添加新项?

我需要保留现有数据.

.net c#

123
推荐指数
12
解决办法
41万
查看次数

是否可以为重载方法使用不同的返回类型?

在方法重载中,是否可以为重载方法设置不同的返回类型?例如,

void foo(int x) ;
int foo(int x,int y);
double foo(String str);
Run Code Online (Sandbox Code Playgroud)

在一般的面向对象编程中,有可能吗?

overloading

29
推荐指数
6
解决办法
8万
查看次数

转换TableOperation.Retrieve的结果

我最近进入了C#/ Azure并遇到了一个我想解决的小问题.该应用程序按预期工作,但我想重构一堆类,因为我确信有一个更简单的解决方案.

我目前有一堆函数可以从Azure中检索实体,这些实体只能在检索到的类型中有所不同,但最好的是我只想要一个这样的类:

public static Object Do(string RowKey, string partitionKey, string tableName)
{
    var theTable = Connect.Initialize(tableName);
    var retrieveOperation = TableOperation.Retrieve(
                                      Base64.EncodeTo64(partitionKey),
                                      Base64.EncodeTo64(RowKey));

    var retrievedResult = theTable.Execute(retrieveOperation);

    if (retrievedResult.Result != null) {
        return retrievedResult.Result;
    }
     throw new ArgumentException(
                 String.Format("{0} not found in the Table", RowKey));
}
Run Code Online (Sandbox Code Playgroud)

这本身就可以工作并检索所需的实体.但是,我无法在没有错误的情况下强制转换返回的Object.

我想要将其强制转换为实现TableEntity-Type的Object Type,并将Result与表中的Result匹配.

我知道我可以用它来表达它

TableOperation.Retrieve<Type>(...)
Run Code Online (Sandbox Code Playgroud)

但我真的想为此目的使用一个函数,这需要我在调用函数时强制转换它.

我怀疑这个问题与结果是Type DynamicTableEntity的事实有关,但我很遗憾为什么.

有没有办法优雅地解决这个问题/是否有办法建立一个参数,保存我想要的类型作为结果?(我尝试使用"Type ..."但这不起作用).

c# azure

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

C# - 函数的返回类型是一个参数

我想知道函数是否可以返回C#中参数中指定的类型值.

这就是我想要做的:

public ReturnValueUndefined function(undefinedType value)
{
    return value;
}
Run Code Online (Sandbox Code Playgroud)

我想使用它,因为我希望我的函数将任何类作为参数并返回相同的类.

谢谢!

c# parameters

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

基于输入数据的自定义返回类型

我有一个方法,它接受一个接口类型并评估类型是什么,并从中我需要返回一个与之相关的类型.但我不确定如何使类型返回灵活.我试过这样的事情:

public static T GridPosition <T>(IReSizeableGrid gridData) {
  if (gridData is Hex) {
    var hexGrid = (HexGrid) gridData;
    return HexLibrary.WorldToHex(WorldPoint(Input.mousePosition, GroundPlane), hexGrid);
  }
  if (gridData is QuadGrid) {
    var quadGrid = (QuadGrid) gridData;
    return Grid.Get(WorldPoint(Input.mousePosition, GroundPlane), quadGrid);
  }
  throw new Exception("Wrong type passed to GridPosition: " + gridData.GetType());
}
Run Code Online (Sandbox Code Playgroud)

但我得到这个错误:

无法隐式转换类型 Hex to T

我在这里使用正确的线路T吗?试图了解如何正确使用它.

.net c# generics

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

如何返回T型?

我有通用方法:

private ILogger _logger; //get logger something else.
public T GetService<T>(ServiceType type)
{
 //how to return _logger?
}
Run Code Online (Sandbox Code Playgroud)

我怎样才能返回T?例如,我有 ILogger:

var logger=GetService<ILogger>(log);
Run Code Online (Sandbox Code Playgroud)

如何实现 GetService 方法?谢谢!

PS 是的,我尝试从 GetService 方法中获取一种或多种类型。例如:Logger,其他一些基础设施类。我知道,ServiceLocator 不是正确的选择(它在很多地方使用),但以我的方式(GetService 将在一个地方使用:为我的插件获取基础设施)我希望它是正常的。

c# generics

3
推荐指数
2
解决办法
2万
查看次数

C# - Generic按参数返回类型的方法

我有这个通用的方法:

    public static T FindObject<T> (this GameObject gameObject, string objectName, Type type)
    {

       var ret = gameObject.GetComponentsInChildren(type).Where(w => w.name == objectName).First();

       return (T)Convert.ChangeType(ret, type);

    }
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

我这样称呼它:

var myVar = UI_POINTS.FindObject("Score", typeof(Text));
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述

但我给出以下错误:

无法从用法中推断出方法'ExtensionMethods.FindObject(GameObject,string,Type)'的类型参数.尝试显式指定类型参数.

为什么他不懂方法调用?

先感谢您.

c# generics methods

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

在c#中返回泛型类型

public class Manager<T> where T: IBallGame
{
T GetManager()
{
//if T is ISoccer return new Soccer()
//if T is IFootball return new Football()

//This wont work. Why?
if (typeof(T) == typeof(ISoccer))
                return new Soccer();
}
}

Interface ISoccer: IBallgame
{
}
class Soccer: ISoccer
{
}
Interface IFootball: IBallgame
{
}
class Football:IFootball
{
}
Run Code Online (Sandbox Code Playgroud)

我已经检查了这个问题如何使方法的返回类型通用?.有没有比Convert.ChangeType()更优雅的东西?

当对类型有约束时,为什么不能返回足球或足球的实例?

c# generics

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

标签 统计

c# ×7

generics ×4

.net ×2

azure ×1

methods ×1

overloading ×1

parameters ×1