我有以下方法(在我的DLL中实现第三方sdk方法):
public void PlaceItem(myItemData item, IRequest request)
{
var item = (ItemClassData)item; //**myItemData is the base class for ItemClassData**
item.Id = 101;
request.Succeeded(item.Id);
}
Run Code Online (Sandbox Code Playgroud)
接口参数如下:
public interface IRequest
{
void Failed(string Error);
void Succeeded(string ItemId);
}
Run Code Online (Sandbox Code Playgroud)
我想从函数中调用此方法.我知道如何在调用此方法时将对象作为第一个参数传递.但是如何传递第二个参数(Interface).任何帮助是极大的赞赏.
c# methods interface parameter-passing interface-implementation
目前我使用以下代码获取硬盘的序列号:
private void GetAllDiskDrives()
{
var searcher = new ManagementObjectSearcher("SELECT * FROM Win32_DiskDrive");
foreach (ManagementObject wmi_HD in searcher.Get())
{
HardDrive hd = new HardDrive();
hd.Model = wmi_HD["Model"].ToString();
hd.InterfaceType = wmi_HD["InterfaceType"].ToString();
hd.SerialNo = wmi_HD.GetPropertyValue("SerialNumber").ToString();//get the serailNumber of diskdrive
HdCollection.Add(hd);
}
}
public class HardDrive
{
public string Model { get; set; }
public string InterfaceType { get; set; }
public string SerialNo { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
这段代码工作正常.
但上面的代码返回所有驱动器.我只想要运行我的软件的特定硬盘驱动器(Not Partition)序列号.
那么,如何获取运行我的软件的硬盘序列号?
在For迭代中,我将一个Task放入其中.每项任务都有自己的工作.代码如下:
List<Task> lst_tsk = new List<Task>();
List<int> lst_item = new List<int> { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
foreach (var item in lst_item)
{
Task tsk = new Task(() =>
{
Console.WriteLine(item);
});
lst_tsk.Add(tsk);
tsk.Start();
}
foreach (var t in lst_tsk)
{
if (t.IsCompleted == false)
t.Wait();
}
Run Code Online (Sandbox Code Playgroud)
我希望它打印出1,2,3,4,5,6,7,8,9,10.但它有效并打破了我的想法.结果是10,10,10,10,10,10,10,10,10,10.
那么如何编辑代码以使事情正确呢?
如何从c#.net控制台应用程序中的单词获取特定页面?
我试过了,
但不幸的是,我的应用程序出错了.
以下是我的代码:
{
object what = WdGoToItem.wdGoToPage;
object which = WdGoToDirection.wdGoToFirst;
object count = 0;
const string fileName = @"C:\..\..\test.doc";
object fileNameAsObject = fileName;
Application wordApplication = new Application();
object readOnly = false;
object missing = System.Reflection.Missing.Value;
wordApplication.Documents.Open(ref fileNameAsObject, ref missing, ref readOnly, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing,
ref missing, ref missing, ref missing, ref missing);
// here on this following line I have got …Run Code Online (Sandbox Code Playgroud) if ( Math.Pow(functionofY, 3.0) > 0.008856 )
{
functionofY = Math.Pow(functionofY, 3);
}
Run Code Online (Sandbox Code Playgroud)
如果我有这个功能,编译器会只计算一次功率吗?或者它是否通过您的代码并找到重复的计算?我觉得编译器有时是一个神奇的黑盒子.
我有一个对象列表(即整数),我想用LINQ聚合子列表.
例如:
原始清单: [ 1, 4, 5, 3, 4, 10, 4, 12 ]
子列表: [ [1,4,5,3], [4,5,3,4], [5,3,4,10], [3,4,10,4], [4,10,4,12] ]
结果(汇总清单): [ 5, 5, 10, 10, 12 ]
我想为包含自身和以下n = 3元素的每个元素创建最大子列表.这是LINQ的可能吗?还是我需要创建自己的聚合机制?
提前谢谢,克里斯蒂安
我知道这可能是一个简单的问题,但我开始迷路了。我用于为强名称(在 Visual Studio 项目属性中)签署程序集的私钥是否与生成 CSR 相关/需要?
从一个网址我看到人们可以像这样实例化界面
class Program
{
static void Main(string[] args)
{
var foo = new IFoo(1);
foo.Do();
}
}
[
ComImport,
Guid("C906C002-B214-40d7-8941-F223868B39A5"),
CoClass(typeof(FooImpl))
]
public interface IFoo
{
void Do();
}
public class FooImpl : IFoo
{
private readonly int i;
public FooImpl(int i)
{
this.i = i;
}
public void Do()
{
Console.WriteLine(i);
}
}
Run Code Online (Sandbox Code Playgroud)
如何写这样var foo = new IFoo(1);寻找指导.谢谢
我的情况是我希望有一个方法可以采用其他方法作为输入.
输入方法将始终具有单个对象作为输入,但对象是不同的.
这或多或少是我需要做的:
public static void takeAnything(Action<object> inputFunc)
{
Console.WriteLine(inputFunc.Method.Name);
}
public static void test1(MyOwnObject input){
// Do stuff with input object
}
public static void test2(MyOtherOwnObject input){
// Do stuff with input object
}
public static void startSystem(){
takeAnything(test1);
takeAnything(test2);
}
Run Code Online (Sandbox Code Playgroud)
在这个虚拟案例中会写出:
test1
test2
Run Code Online (Sandbox Code Playgroud)
我只是不能让这个工作,所以任何帮助将非常感激.
编辑
我不知道这是否可行,也许不是,但重要的是呼叫只是takeAnything(test1),不是takeAnything<MyOwnObject>(test1)或其他任何东西.
我有一个可以从其他两个表单打开的表单 (form3)。表格 1 和表格 2。
我怎样才能得到哪一个是form3的父级?