我想用自己的代码替换3d party API中的一行代码.
我的意思是这是一个值得怀疑的做法,但我需要修复他们的错误.
我想知道如何实际获取正在运行的文件的名称而不是程序集名称,而是在C#.NET中获取文件的名称,
我试过了
System.Reflection.Assembly.GetEntryAssembly().GetName().Name
Run Code Online (Sandbox Code Playgroud)
这给出了程序集的名称,但我正在寻找exe文件的名称.
谢谢!
在加载某些程序集时,ASP.NET是否将所有依赖项程序集(位于BIN文件夹中)加载到默认的appdomain?或者它将"按需"加载(当CLR需要依赖程序集中的某种类型时)?
我修改了程序集的字节码以摆脱错误,现在当我尝试使用它时,我得到一个InvalidProgramException.我所做的就是用NOPS代替这个代码:
catch (Exception exception1)
{
Exception exception = exception1;
if (exception as InvalidValueException == null)
{
throw new InvalidGenerationException(2);
}
else
{
throw exception;
}
}
Run Code Online (Sandbox Code Playgroud)
IL之前:
catch [mscorlib]System.Exception
{
IL_022f: stloc.s exception
IL_0231: ldloc.s exception
IL_0233: isinst Custom.InvalidValueException
IL_0238: brfalse.s IL_023d
IL_023a: ldloc.s exception
IL_023c: throw
IL_023d: ldc.i4.1
IL_023e: newobj instance void Custom.InvalidGenerationException ...
IL_0243: throw
}
Run Code Online (Sandbox Code Playgroud)
IL之后:
catch [mscorlib]System.Exception
{
IL_022f: nop
IL_0230: nop
IL_0231: nop
IL_0232: nop
IL_0233: nop
IL_0234: nop
IL_0235: nop
IL_0236: nop
IL_0237: nop …
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Microsoft Office Interops版本11,.NET 2.0修复使用Visual Studio 2003创建的此遗留应用程序.我正在尝试在Visual Studio Express 2010中修复它以引用Interops版本14,.NET 4.0 - 正如我之前关于StackOverflow的问题中所述,遗留应用程序在Windows 7中工作正常,但在我关闭它之后,Microsoft Office产品是当我尝试使用它们时崩溃.
但是,当我修复VS2010中的引用(删除旧的v.11 Interops,添加新的v.14 Interops)然后尝试发布应用程序时,我得到的错误就像
'Microsoft.Office.Interop.Word.System does not contain a definition for IO'
Run Code Online (Sandbox Code Playgroud)
看起来VS2010在引用Word命名空间时没有看到我的System命名空间被使用?当我删除了
using Microsoft.Office.Interop.Word
Run Code Online (Sandbox Code Playgroud)
命名空间然后尝试发布,上面的错误消失了,我只得到与缺少的Word引用相关的预期错误
The type or namespace name '_Document' could not be found (are you missing a using directive or an assembly reference?)
Run Code Online (Sandbox Code Playgroud)
我已经在参考中包含了System.dll,所以我不确定发生了什么?谢谢阅读!
编辑:我使Office Interops的"嵌入式互操作类型"为假.那可能修复了一些错误?但是:Visual Studio仍然将任何系统引用解释为"Microsoft.Office.Interop.Word.System",这不是我想要的.这个错误似乎是现在的主导:
The type name 'Windows' does not exist in the type 'Microsoft.Office.Interop.Word.System'
Run Code Online (Sandbox Code Playgroud) 我不确定这是否可行,而且我在C#.NET中使用程序集还是很新的.
我想要做的是在提供该类的字符串名称时创建一个类的实例.像这样的东西:
using MyAssembly;
namespace MyNameSpace
{
Class MyClass
{
int MyValue1;
int MyValue2;
public MyClass(string myTypeName)
{
foreach(Type type in MyAssembly)
{
if((string)type == myTypeName)
{
//create a new instance of the type
}
}
AssignInitialValues(//the type created above)
}
//Here I use an abstract type which the type above inherits from
private void AssignInitialValues(AbstractType myClass)
{
this.value1 = myClass.value1;
this.value2 = myClass.value2;
}
}
}
Run Code Online (Sandbox Code Playgroud)
显然你无法将字符串与类型进行比较,但它说明了我正在尝试做的事情:在提供的字符串中创建一个在不同程序集中找到的类型.
有什么想法吗?
编辑:
尝试后:
var myObject = (AbstractType) Activator.CreateInstance(null, myTypeName);
AssignInitialValues(myObject);
Run Code Online (Sandbox Code Playgroud)
我收到了一些错误:
我为一个程序集写了一个类,它只能由这个程序集中的工厂实例化.
大会外:
MyClass myClass = factoryInstance.GetInstanceOfMyClass();
Run Code Online (Sandbox Code Playgroud)
应该禁止在外面实施这门课程
禁止:
MyClass myClass = new MyClass();
Run Code Online (Sandbox Code Playgroud)
我见过一些复杂的方法.有一种我想念的简单方法吗?
我正在研究的项目将为大量类生成代码 - 预计会有数百到数千个.在生成时不知道实际上将访问多少这些类.
生成的类可以(1)全部存在于单个程序集中(或者可能是少数几个程序集),这些程序集将在脚趾消耗进程启动时加载.
...或者(2)我可以为每个类生成一个程序集,就像Java将每个类编译为单个*.class
二进制文件一样,然后提出一种按需加载程序集的机制.
问题是:哪种情况会产生更好的(记忆和时间)表现?
我的直觉是,对于情况(1),加载时间和使用的存储器与构成单个整体组件的类的数量成正比.OTOH,案例(2)带有其复杂性.
如果您知道有关加载程序集内部的任何资源,特别是调用的代码(如果有的话!?)和分配的内存(新加载的程序集的簿记),请分享它们.
为了重新产生问题,我创建了这样一个简单的类,这个文件将被编译为"SourceDLL.dll".
namespace SourceDll
{
public class Class1
{
static public int Add(int b, int c)
{
return b + c;
}
}
}
Run Code Online (Sandbox Code Playgroud)
然后我使用Mono.Cecil修改它,我只是打开并保存回来.
namespace InstrumentSystemDll
{
class Program
{
static void Main(string[] args)
{
var fileName = args[0];
var assembly = AssemblyDefinition.ReadAssembly(string.Format(@"..\..\..\Dependences\{0}.dll", fileName));
Console.WriteLine(string.Format(@"..\..\..\Dependences\{0}.Patched.dll", fileName));
assembly.Write(string.Format(@"..\..\..\Dependences\{0}.Patched.dll", fileName));
}
}
}
Run Code Online (Sandbox Code Playgroud)
我有一个修改过的"SourceDll.Patched.dll"文件,然后我尝试使用这个文件.创建了一个consloe应用程序并引用了"SourceDll.Patched.dll".
namespace TestInstrumentDll
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(Class1.Add(1, 1));
}
}
Run Code Online (Sandbox Code Playgroud)
}
不幸的是,我有这样的错误:
未处理的异常:System.IO.FileNotFoundException:无法加载文件或包含'SourceDll,Version = 1.0.0.0,Culture = neutral,PublicKeyToken = null'或其依赖项之一.该系统找不到指定的文件.在TestInstrumentDll.Program.Main(String [] args)
如果我切换到原始SourceDll.dll,它工作并打印一个数字"2",如我们所料.
我有一个asp.net web api应用程序,其中我有这一行:
using System.Web.Http.Results;
Run Code Online (Sandbox Code Playgroud)
我的问题是命名空间无法识别,我验证system.Web.Http
了bin文件夹中存在的dll !!
所以我需要知道:
.net asp.net-mvc .net-assembly asp.net-web-api system.web.http
.net-assembly ×10
.net ×7
c# ×5
reflection ×2
appdomain ×1
asp.net-mvc ×1
assemblies ×1
bytecode ×1
factory ×1
il ×1
mono ×1
mono.cecil ×1
ms-office ×1
performance ×1